diff options
author | Yaxun (Sam) Liu <yaxun.liu@amd.com> | 2025-02-27 10:41:04 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-27 10:41:04 -0500 |
commit | 240f2269ffdbd96e68b2159ae537d8486164c10c (patch) | |
tree | c07c7fa87f8205de45967c612089778fcf23bf57 /clang/lib/CodeGen | |
parent | c630de934ca2c8381bdd60268179bc14f792ec19 (diff) | |
download | llvm-240f2269ffdbd96e68b2159ae537d8486164c10c.zip llvm-240f2269ffdbd96e68b2159ae537d8486164c10c.tar.gz llvm-240f2269ffdbd96e68b2159ae537d8486164c10c.tar.bz2 |
Add clang atomic control options and attribute (#114841)
Add option and statement attribute for controlling emitting of
target-specific
metadata to atomicrmw instructions in IR.
The RFC for this attribute and option is
https://discourse.llvm.org/t/rfc-add-clang-atomic-control-options-and-pragmas/80641,
Originally a pragma was proposed, then it was changed to clang
attribute.
This attribute allows users to specify one, two, or all three options
and must be applied
to a compound statement. The attribute can also be nested, with inner
attributes
overriding the options specified by outer attributes or the target's
default
options. These options will then determine the target-specific metadata
added to atomic
instructions in the IR.
In addition to the attribute, three new compiler options are introduced:
`-f[no-]atomic-remote-memory`, `-f[no-]atomic-fine-grained-memory`,
`-f[no-]atomic-ignore-denormal-mode`.
These compiler options allow users to override the default options
through the
Clang driver and front end. `-m[no-]unsafe-fp-atomics` is aliased to
`-f[no-]ignore-denormal-mode`.
In terms of implementation, the atomic attribute is represented in the
AST by the
existing AttributedStmt, with minimal changes to AST and Sema.
During code generation in Clang, the CodeGenModule maintains the current
atomic options,
which are used to emit the relevant metadata for atomic instructions.
RAII is used
to manage the saving and restoring of atomic options when entering
and exiting nested AttributedStmt.
Diffstat (limited to 'clang/lib/CodeGen')
-rw-r--r-- | clang/lib/CodeGen/CGStmt.cpp | 5 | ||||
-rw-r--r-- | clang/lib/CodeGen/CodeGenFunction.h | 42 | ||||
-rw-r--r-- | clang/lib/CodeGen/CodeGenModule.cpp | 3 | ||||
-rw-r--r-- | clang/lib/CodeGen/CodeGenModule.h | 8 | ||||
-rw-r--r-- | clang/lib/CodeGen/Targets/AMDGPU.cpp | 20 |
5 files changed, 67 insertions, 11 deletions
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp index 7368df1..e56ba6c 100644 --- a/clang/lib/CodeGen/CGStmt.cpp +++ b/clang/lib/CodeGen/CGStmt.cpp @@ -786,6 +786,7 @@ void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) { HLSLControlFlowHintAttr::Spelling flattenOrBranch = HLSLControlFlowHintAttr::SpellingNotCalculated; const CallExpr *musttail = nullptr; + const AtomicAttr *AA = nullptr; for (const auto *A : S.getAttrs()) { switch (A->getKind()) { @@ -816,6 +817,9 @@ void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) { Builder.CreateAssumption(AssumptionVal); } } break; + case attr::Atomic: + AA = cast<AtomicAttr>(A); + break; case attr::HLSLControlFlowHint: { flattenOrBranch = cast<HLSLControlFlowHintAttr>(A)->getSemanticSpelling(); } break; @@ -827,6 +831,7 @@ void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) { SaveAndRestore save_noconvergent(InNoConvergentAttributedStmt, noconvergent); SaveAndRestore save_musttail(MustTailCall, musttail); SaveAndRestore save_flattenOrBranch(HLSLControlFlowAttr, flattenOrBranch); + CGAtomicOptionsRAII AORAII(CGM, AA); EmitStmt(S.getSubStmt(), S.getAttrs()); } diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h index 8c5362b..7c0d6c3 100644 --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -834,6 +834,48 @@ public: }; FPOptions CurFPFeatures; + class CGAtomicOptionsRAII { + public: + CGAtomicOptionsRAII(CodeGenModule &CGM_, AtomicOptions AO) + : CGM(CGM_), SavedAtomicOpts(CGM.getAtomicOpts()) { + CGM.setAtomicOpts(AO); + } + CGAtomicOptionsRAII(CodeGenModule &CGM_, const AtomicAttr *AA) + : CGM(CGM_), SavedAtomicOpts(CGM.getAtomicOpts()) { + if (!AA) + return; + AtomicOptions AO = SavedAtomicOpts; + for (auto Option : AA->atomicOptions()) { + switch (Option) { + case AtomicAttr::remote_memory: + AO.remote_memory = true; + break; + case AtomicAttr::no_remote_memory: + AO.remote_memory = false; + break; + case AtomicAttr::fine_grained_memory: + AO.fine_grained_memory = true; + break; + case AtomicAttr::no_fine_grained_memory: + AO.fine_grained_memory = false; + break; + case AtomicAttr::ignore_denormal_mode: + AO.ignore_denormal_mode = true; + break; + case AtomicAttr::no_ignore_denormal_mode: + AO.ignore_denormal_mode = false; + break; + } + } + CGM.setAtomicOpts(AO); + } + ~CGAtomicOptionsRAII() { CGM.setAtomicOpts(SavedAtomicOpts); } + + private: + CodeGenModule &CGM; + AtomicOptions SavedAtomicOpts; + }; + public: /// ObjCEHValueStack - Stack of Objective-C exception values, used for /// rethrows. diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 1b7d0ac..3caa79b 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -342,7 +342,8 @@ CodeGenModule::CodeGenModule(ASTContext &C, PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags), Target(C.getTargetInfo()), ABI(createCXXABI(*this)), VMContext(M.getContext()), VTables(*this), StackHandler(diags), - SanitizerMD(new SanitizerMetadata(*this)) { + SanitizerMD(new SanitizerMetadata(*this)), + AtomicOpts(Target.getAtomicOpts()) { // Initialize the type cache. Types.reset(new CodeGenTypes(*this)); diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index c6f6fd5..4a269f6 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -676,6 +676,8 @@ private: std::optional<PointerAuthQualifier> computeVTPointerAuthentication(const CXXRecordDecl *ThisClass); + AtomicOptions AtomicOpts; + public: CodeGenModule(ASTContext &C, IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS, const HeaderSearchOptions &headersearchopts, @@ -691,6 +693,12 @@ public: /// Finalize LLVM code generation. void Release(); + /// Get the current Atomic options. + AtomicOptions getAtomicOpts() { return AtomicOpts; } + + /// Set the current Atomic options. + void setAtomicOpts(AtomicOptions AO) { AtomicOpts = AO; } + /// Return true if we should emit location information for expressions. bool getExpressionLocationsEnabled() const; diff --git a/clang/lib/CodeGen/Targets/AMDGPU.cpp b/clang/lib/CodeGen/Targets/AMDGPU.cpp index dc45def..9d29f31 100644 --- a/clang/lib/CodeGen/Targets/AMDGPU.cpp +++ b/clang/lib/CodeGen/Targets/AMDGPU.cpp @@ -585,19 +585,19 @@ void AMDGPUTargetCodeGenInfo::setTargetAtomicMetadata( AtomicInst.setMetadata(llvm::LLVMContext::MD_noalias_addrspace, ASRange); } - if (!RMW || !CGF.getTarget().allowAMDGPUUnsafeFPAtomics()) + if (!RMW) return; - // TODO: Introduce new, more controlled options that also work for integers, - // and deprecate allowAMDGPUUnsafeFPAtomics. - llvm::AtomicRMWInst::BinOp RMWOp = RMW->getOperation(); - if (llvm::AtomicRMWInst::isFPOperation(RMWOp)) { - llvm::MDNode *Empty = llvm::MDNode::get(CGF.getLLVMContext(), {}); + AtomicOptions AO = CGF.CGM.getAtomicOpts(); + llvm::MDNode *Empty = llvm::MDNode::get(CGF.getLLVMContext(), {}); + if (!AO.getOption(clang::AtomicOptionKind::FineGrainedMemory)) RMW->setMetadata("amdgpu.no.fine.grained.memory", Empty); - - if (RMWOp == llvm::AtomicRMWInst::FAdd && RMW->getType()->isFloatTy()) - RMW->setMetadata("amdgpu.ignore.denormal.mode", Empty); - } + if (!AO.getOption(clang::AtomicOptionKind::RemoteMemory)) + RMW->setMetadata("amdgpu.no.remote.memory", Empty); + if (AO.getOption(clang::AtomicOptionKind::IgnoreDenormalMode) && + RMW->getOperation() == llvm::AtomicRMWInst::FAdd && + RMW->getType()->isFloatTy()) + RMW->setMetadata("amdgpu.ignore.denormal.mode", Empty); } bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { |