Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions llvm/docs/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ Changes to the Python bindings
Changes to the C API
--------------------

* Added the following helper functions to configure thread local storage
settings for `LLVMCreateTargetMachineWithOptions`:
* `LLVMTargetMachineOptionsSetEmulatedTLS`
* `LLVMTargetMachineOptionsSetEnableTLSDESC`

Changes to the CodeGen infrastructure
-------------------------------------

Expand Down
8 changes: 8 additions & 0 deletions llvm/include/llvm-c/TargetMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ LLVM_C_ABI void
LLVMTargetMachineOptionsSetCodeModel(LLVMTargetMachineOptionsRef Options,
LLVMCodeModel CodeModel);

LLVM_C_ABI void
LLVMTargetMachineOptionsSetEmulatedTLS(LLVMTargetMachineOptionsRef Options,
LLVMBool EmulatedTLS);

LLVM_C_ABI void
LLVMTargetMachineOptionsSetEnableTLSDESC(LLVMTargetMachineOptionsRef Options,
LLVMBool EnableTLSDESC);

/**
* Create a new llvm::TargetMachine.
*
Expand Down
14 changes: 14 additions & 0 deletions llvm/lib/Target/TargetMachineC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct LLVMTargetMachineOptions {
std::optional<Reloc::Model> RM;
std::optional<CodeModel::Model> CM;
bool JIT;
bool EmulatedTLS;
bool EnableTLSDESC;
Comment on lines +43 to +44
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm... I think these need to be std::optional and only used if the corresponding functions are called? Otherwise we're not going to respect hasDefaultEmulatedTLS and hasDefaultTLSDESC anymore, right?

Copy link
Author

Choose a reason for hiding this comment

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

I investigated a bit: LLVMCreateTargetMachineWithOptions uses the default llvm::TargetOptions constructor that explicitly initializes both options to false already. Setting them again to false again is a NOOP.

I understand that the frontend is responsible to call hasDefaultEmulatedTLS or hasDefaultTLSDESC as a fallback value when unspecified. Clang appears to do that.

Now, I could change both options to be std::optional and have LLVMCreateTargetMachineWithOptions set the options when they have been explicit set, and fallback to hasDefaultEmulatedTLS and hasDefaultTLSDESC otherwise.

};

} // namespace llvm
Expand Down Expand Up @@ -197,12 +199,24 @@ void LLVMTargetMachineOptionsSetCodeModel(LLVMTargetMachineOptionsRef Options,
unwrap(Options)->CM = CM;
}

void LLVMTargetMachineOptionsSetEmulatedTLS(LLVMTargetMachineOptionsRef Options,
LLVMBool EmulatedTLS) {
unwrap(Options)->EmulatedTLS = EmulatedTLS;
}

void LLVMTargetMachineOptionsSetEnableTLSDESC(
LLVMTargetMachineOptionsRef Options, LLVMBool EnableTLSDESC) {
unwrap(Options)->EnableTLSDESC = EnableTLSDESC;
}

LLVMTargetMachineRef
LLVMCreateTargetMachineWithOptions(LLVMTargetRef T, const char *TripleStr,
LLVMTargetMachineOptionsRef Options) {
auto *Opt = unwrap(Options);
TargetOptions TO;
TO.MCOptions.ABIName = Opt->ABI;
TO.EmulatedTLS = Opt->EmulatedTLS;
TO.EnableTLSDESC = Opt->EnableTLSDESC;
return wrap(unwrap(T)->createTargetMachine(Triple(TripleStr), Opt->CPU,
Opt->Features, TO, Opt->RM,
Opt->CM, Opt->OL, Opt->JIT));
Expand Down
2 changes: 2 additions & 0 deletions llvm/unittests/Target/TargetMachineOptionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ TEST(TargetMachineCTest, TargetMachineOptions) {
LLVMTargetMachineOptionsSetCodeGenOptLevel(Options, LLVMCodeGenLevelNone);
LLVMTargetMachineOptionsSetRelocMode(Options, LLVMRelocStatic);
LLVMTargetMachineOptionsSetCodeModel(Options, LLVMCodeModelKernel);
LLVMTargetMachineOptionsSetEmulatedTLS(Options, true);
LLVMTargetMachineOptionsSetEnableTLSDESC(Options, true);

LLVMDisposeTargetMachineOptions(Options);
}
Expand Down