Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LLVM] Create LLVM scope object for use with LLVM libraries #12140

Merged
merged 7 commits into from
Aug 3, 2022
Merged

[LLVM] Create LLVM scope object for use with LLVM libraries #12140

merged 7 commits into from
Aug 3, 2022

Conversation

kparzysz-quic
Copy link
Contributor

This implements RFC 80. See apache/tvm-rfcs#83.

Summary of changes:

  • Created an LLVMScope class. Uses of LLVM functions and data structures should be contained within the lifetime of an object of this class. LLVMScope object contains LLVMContext, and implements member functions to deserialize an llvm::Module.
  • Created an LLVMTarget class. Once an LLVMScope object has been created, an object of LLVMTarget class can be created from TVM target string, or Target object for "llvm" target. Once LLVM command line flags are added to the "llvm" target, one of the goals of this object will be to save/restore relevant LLVM global state. Another objective for the LLVMTarget object is to be a single location for all LLVM-related compilation structures and options (such as TargetMachine, FastMathFlags, etc.)

This implements RFC 80. See apache/tvm-rfcs#83.

Summary of changes:
- Created an `LLVMScope` class. Uses of LLVM functions and data struc-
tures should be contained within the lifetime of an object of this class.
LLVMScope object contains LLVMContext, and implements member functions
to deserialize an llvm::Module.
- Created an `LLVMTarget` class. Once an LLVMScope object has been
created, an object of LLVMTarget class can be created from TVM target
string, or Target object for "llvm" target. Once LLVM command line flags
are added to the "llvm" target, one of the goals of this object will be
to save/restore relevant LLVM global state. Another objective for the
LLVMTarget object is to be a single location for all LLVM-related
compilation structures and options (such as TargetMachine, FastMathFlags,
etc.)
Copy link
Contributor

@areusch areusch left a comment

Choose a reason for hiding this comment

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

thanks @kparzysz-quic this looks like a pretty good refactor. ccing a few folks who might be affected @manupa-arm @Mousius @tqchen @junrushao1994 @csullivan @comaniac @Hzfengsy

src/target/llvm/llvm_scope.h Outdated Show resolved Hide resolved

namespace {
bool InitializeLLVM() {
static std::atomic_flag initialized = ATOMIC_FLAG_INIT;
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we need the atomic here? i don't think we typically support multithreaded operation

Copy link
Contributor Author

@kparzysz-quic kparzysz-quic Jul 25, 2022

Choose a reason for hiding this comment

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

The previous initialization code (in llvm_common.cc) had an atomic flag in it (although in a bit more complex scheme), so I kept it. If you still think it should be removed, let me know---I have no problem changing it.

Edit: llvm_common.cc is not shown in the diff. You need to show it manually for the link to work.

src/target/llvm/llvm_scope.cc Outdated Show resolved Hide resolved
src/target/llvm/codegen_llvm.cc Outdated Show resolved Hide resolved
Krzysztof Parzyszek added 4 commits July 25, 2022 06:49
Move empty implementations of EnterWithScope/ExitWithScope to header
since it helps see that these functions are only stubs.
@kparzysz-quic
Copy link
Contributor Author

Pinging all interested parties... Are there any concerns that haven't been addressed?

@kparzysz-quic
Copy link
Contributor Author

Is it ok to merge?

@kparzysz-quic
Copy link
Contributor Author

@tqchen: are you ok with this?

@junrushao
Copy link
Member

@tqchen would you like to review this? Thanks a lot!

@tqchen
Copy link
Member

tqchen commented Aug 3, 2022

Sorry for delayed reply. Approving as it is a good chunk of improvement. It would be great to discuss naming and documentation a bit.

When I see LLVMScope, I was expecting LLVM to taken in-effect. But in reality, we can have multiple LLVMScope object, one per LLVMModule. The API kicks in when we do

With<LLVMTarget> target(scope, ...)

So LLVMScope is not exactly an unique RAII object. Sorry that I missed it when reading the RFC but it becomes obvious when reading implementation of LLVMModule.

Perhaps we should consider clarify and rename (it should be an LLVMContext?) in this case.

Followup actions:

  • Clarify what happens when multiple LLVMScope are created (one for each LLVMModule)
  • Clarify when does RAII config takes in effect (With target seems like to be a good place)

@kparzysz-quic
Copy link
Contributor Author

Originally the scope took effect immediately, but there was an ordering problem with deserializing LLVM IR: LLVM flags would only be known once we've made LLVM calls, and once we've created an LLVM context. So later in the RFC discussion I proposed separating the scope into two parts: one part that encloses the LLVM context creation, and one that saves/restores LLVM options.

I'd be happy to make further improvements to this, as we decide going on in the future.

@tqchen
Copy link
Member

tqchen commented Aug 3, 2022

Thanks @kparzysz-quic , my guess is that we just need naming and documentation clarifications. e.g. the perhaps we can change name of LLVMScope to LLVMContext to capture the fact that it is " LLVM context creation," and the scoping happens in With<LLLVMTarget>

@kparzysz-quic
Copy link
Contributor Author

[...] perhaps we can change name of LLVMScope to LLVMContext

I'm a bit concerned that it could create confusion given that LLVMContext is name of LLVM type. Let me think a bit, maybe I can come up with a different name.

@kparzysz-quic
Copy link
Contributor Author

What do you think about LLVMInstance?

@tqchen
Copy link
Member

tqchen commented Aug 3, 2022

LLVMInstance sounds good

@kparzysz-quic kparzysz-quic merged commit 2e02cf7 into apache:main Aug 3, 2022
@kparzysz-quic kparzysz-quic deleted the llvm-scope-target branch August 3, 2022 23:17
@kparzysz-quic
Copy link
Contributor Author

I edited the commit message to use LLVMInstance instead of LLVMScope in the summary.

xinetzone pushed a commit to daobook/tvm that referenced this pull request Nov 25, 2022
…2140)

This implements RFC 80. See apache/tvm-rfcs#83.

Summary of changes:
- Created an `LLVMInstance` class. Uses of LLVM functions and data struc-
tures should be contained within the lifetime of an object of this class.
LLVMInstance object contains LLVMContext, and implements member functions
to deserialize an llvm::Module.
- Created an `LLVMTarget` class. Once an LLVMInstance object has been
created, an object of LLVMTarget class can be created from TVM target
string, or Target object for "llvm" target. Once LLVM command line flags
are added to the "llvm" target, one of the goals of this object will be
to save/restore relevant LLVM global state. Another objective for the
LLVMTarget object is to be a single location for all LLVM-related
compilation structures and options (such as TargetMachine, FastMathFlags,
etc.)
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.

4 participants