Skip to content

Create explicit compiler C API libraries and enable dynamic linking on all platforms - #11285

Merged
stellaraccident merged 4 commits into
mainfrom
sympathy
Nov 24, 2022
Merged

stellaraccident merged 4 commits into
mainfrom
sympathy

Conversation

@stellaraccident

Copy link
Copy Markdown
Collaborator

This is the first step of a multi-part sequence that will:

  • Rebuild the compiler/API directory into its final form.
  • Move the compiler python API to its correct location (bindings/python).
  • Enable dynamic linking project wide of all compiler tools.

This first step leaves the following undone:

  • Python API stays where it is (there are a lot of fiddly path dependencies there and I opted to handle that in isolation).
  • Sets everything up in an API2 which will eventually be renamed to API.
  • Just gets the stub of what is needed for a delay loaded embedding API -- the full thing will follow and be integrated into PJRT for Jax and TensorFlow serving on the OSS side.
  • The project local lld is still building static, even though the API includes it in the shared library. Was just trying to keep the patch size down vs there being a fundamental problem.

In the new state, there is a libIREECompiler.so.0 (or IREECompiler.dll on Windows) which contains both the in-process APIs and the tool entry-points. The following tools have been reworked to link against the shared library:

  • iree-compile
  • iree-lld (Python side only)
  • iree-opt
  • iree-mlir-lsp-server

In addition, some API tests which were doing expensive static linking now link dynamically. All said, this saves ~4-5x multi-gigabyte compiler tool linking steps and should be a pretty substantial build iteration improvement. I wasn't originally going to include iree-opt and iree-mlir-lsp-server in the shared build, but based on an experiment, they contribute negligible size (really just a few extra source files when linked together), and the productivity and usability savings (i.e. we can now distribute those at negligible cost) made me decide it was worth it.

I don't know what to do about iree-run-mlir. I think I can refactor it into something based on the compiler API that then links the runtime. But that is for later.

I took some steps to ensure this will work on Windows and MacOS but haven't finished verifying.

…n all platforms.

This is the first step of a multi-part sequence that will:

* Rebuild the compiler/API directory into its final form.
* Move the compiler python API to its correct location (bindings/python).
* Enable dynamic linking project wide of all compiler tools.

This first step leaves the following undone:

* Python API stays where it is (there are a lot of fiddly path dependencies there and I opted to handle that in isolation).
* Sets everything up in an API2 which will eventually be renamed to API.
* Just gets the stub of what is needed for a delay loaded embedding API -- the full thing will follow and be integrated into PJRT for Jax and TensorFlow serving on the OSS side.
* The project local lld is still building static, even though the API includes it in the shared library. Was just trying to keep the patch size down vs there being a fundamental problem.

In the new state, there is a libIREECompiler.so.0 (or IREECompiler.dll on Windows) which contains both the in-process APIs and the tool entry-points. The following tools have been reworked to link against the shared library:

* iree-compile
* iree-lld (Python side only)
* iree-opt
* iree-mlir-lsp-server

In addition, some API tests which were doing expensive static linking now link dynamically. All said, this saves ~4-5x multi-gigabyte compiler tool linking steps and should be a pretty substantial build iteration improvement. I wasn't originally going to include iree-opt and iree-mlir-lsp-server in the shared build, but based on an experiment, they contribute negligible size (really just a few extra source files when linked together), and the productivity and usability savings (i.e. we can now distribute those at negligible cost) made me decide it was worth it.

I don't know what to do about iree-run-mlir. I think I can refactor it into something based on the compiler API that then links the runtime. But that is for later.

I took some steps to ensure this will work on Windows and MacOS but haven't finished verifying.

@benvanik benvanik left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me! May want to get one more signoff from someone who may be responsible for integrating this into google3 - though because this is largely forked in API2 I suspect things may not break there immediately and there's leeway for iteration.

@stellaraccident

Copy link
Copy Markdown
Collaborator Author

This looks great to me! May want to get one more signoff from someone who may be responsible for integrating this into google3 - though because this is largely forked in API2 I suspect things may not break there immediately and there's leeway for iteration.

I am reasonably confident that this improves the google3 integration side as more of the magic bits that were only done there and were done differently on the OSS/CMake side are now unified and guaranteed working at head. Bazel is still static linking tools, and it will continue to do so because it lacks any real facility for managing cross platform dynamic/runtime stuff (and the bug has been open for years without update) -- I have no plans to change Bazel to dynamic linking tools. That would be the big thing that would make it hard to support. I will be slowly extending it in follow-ons to support delay-loading a shared library plugin on that side, which will have some touch points internally but should be in the "new stuff" category enough that it isn't disruptive and can be fixed forward. I ran the basic idea by Geoffrey...

@benvanik

Copy link
Copy Markdown
Collaborator

image

@stellaraccident
stellaraccident merged commit 7ec09ce into main Nov 24, 2022
@stellaraccident
stellaraccident deleted the sympathy branch November 24, 2022 19:13
@bjacob

bjacob commented Nov 25, 2022

Copy link
Copy Markdown
Collaborator

There is a postsubmit Windows test failure on my latest merged PR: https://github.com/iree-org/iree/actions/runs/3549048343 .

The log is unhelpful (AFAICS), just says

The following tests FAILED:
    648 - iree/compiler/API2/test/api-test-binary (Exit code 0xc0000135
)

Exit code 0xc0000135 is STATUS_DLL_NOT_FOUND

I wonder if the root cause might be this PR, but it's hard to tell, because the Windows postsubmit is skipped on this PR, and even when I hit "re-run all jobs" it skips it again:
https://github.com/iree-org/iree/actions/runs/3537484199

@GMNGeoffrey

Copy link
Copy Markdown
Member

The full Windows build is currently postsubmit only:

if: needs.setup.outputs.should-run == 'true' && needs.setup.outputs.ci-stage == 'postsubmit'

@ScottTodd do you think we could promote it to presubmit now?

@stellaraccident

Copy link
Copy Markdown
Collaborator Author

As noted on discord, should be a simple fix and I'll fix it forward. Glad we have any CI for it, frankly :)

@ScottTodd

Copy link
Copy Markdown
Member

@ScottTodd do you think we could promote it to presubmit now?

Yep. I'd still like to see if we can improve the repository and submodule checkout speed though... 6-10 minutes just to get the code is pretty slow :(

@stellaraccident

stellaraccident commented Nov 25, 2022 •

Copy link
Copy Markdown
Collaborator Author

@ScottTodd do you think we could promote it to presubmit now?

Yep. I'd still like to see if we can improve the repository and submodule checkout speed though... 6-10 minutes just to get the code is pretty slow :(

We can just tack it onto my 20 year old bug "Can you please, please replace NTFS with a modern file system" :)

@stellaraccident

Copy link
Copy Markdown
Collaborator Author

Fixed Windows: #11298

# "@llvm-project//mlir:IR" -> "MLIRIR"
# "@llvm-project//mlir:Pass" -> "MLIRPass"
return ["MLIR" + target.rsplit(":")[-1]]
# MLIR does not have header-only targets apart from the libraries. Here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we fix this upstream?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make an issue with all of these little nits. Should fix them in one go.

self.cc_test(deps=deps + ["//runtime/src:runtime_defines"], **kwargs)

def iree_compiler_cc_test(self, deps=[], **kwargs):
self.cc_test(deps=deps + ["//compiler/src:defs"], **kwargs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm I think we should be encoding this in the CMake, not the Bazel->CMake (same for the runtime one above)

Comment on lines +37 to +41
"@llvm-project//lld:COFF": ["lldCOFF"],
"@llvm-project//lld:Common": ["lldCommon"],
"@llvm-project//lld:ELF": ["lldELF"],
"@llvm-project//lld:MachO": ["lldMachO"],
"@llvm-project//lld:Wasm": ["lldWasm"],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These look pretty standardized. We should handle it as a function with only lld special cased here (I'm still a bit suspicious of the lld special case also).

"@llvm-project//mlir:ShapeTransforms": ["MLIRShapeOpsTransforms"],
"@llvm-project//mlir:ToLLVMIRTranslation": ["MLIRTargetLLVMIRExport"],
"@llvm-project//mlir:mlir-translate": ["mlir-translate"],
"@llvm-project//mlir:MlirLspServerLib": ["MLIRLspServerLib"],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sigh. We should strip a leading "Mlir" from the Bazel targets in the conversion (and also fix this upstream)

* `iree-compiler-backend` Python wheel and source distributions, providing
the `iree.compiler_backend` Python packages.
* Compiler C-API source and binary tarballs (future), with some CLI tools.
This directory is being refactored into API2. Currently, it only contains

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should probably still leave the documentation until it's actually deleted.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should probably still leave the documentation until it's actually deleted.

Was my thought too. Until I read it and realized it was hopelessly wrong and better deleted.

# We defer it to the compiler/ directory so that cmake_install.cmake targets
# are all self contained to the compiler, which is the most common spanning
# parent.
cmake_language(EVAL CODE "

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😱

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Right reaction. But I copied this from some code that I think Scott wrote, so I can't claim responsibility for this hack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants