Skip to content

Commit b099709

Browse files
authored
[StackProtector] Do not emit the stack protector on GPU architectures (#70799)
Summary: This patch changes the code generation to not emit the stack protector metadata on unsupported architectures. The issue was caused by system toolchains emitting stack protector option by default which would lead to errors when compiling for the GPU. I elected to change the code generation as we may want to update this in the future so we should keep the `clang` Driver code common. Although the user can use some combination of `-Xarch-device -fno-stack-protector` to override this, it is very irritating to do when we shouldn't emit this incompatible IR anyway. Fixes: #65911
1 parent d402645 commit b099709

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,14 @@ static void setVisibilityFromDLLStorageClass(const clang::LangOptions &LO,
761761
}
762762
}
763763

764+
static bool isStackProtectorOn(const LangOptions &LangOpts,
765+
const llvm::Triple &Triple,
766+
clang::LangOptions::StackProtectorMode Mode) {
767+
if (Triple.isAMDGPU() || Triple.isNVPTX())
768+
return false;
769+
return LangOpts.getStackProtector() == Mode;
770+
}
771+
764772
void CodeGenModule::Release() {
765773
Module *Primary = getContext().getCurrentNamedModule();
766774
if (CXX20ModuleInits && Primary && !Primary->isHeaderLikeModule())
@@ -2296,13 +2304,13 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
22962304
if (D && D->hasAttr<NoStackProtectorAttr>())
22972305
; // Do nothing.
22982306
else if (D && D->hasAttr<StrictGuardStackCheckAttr>() &&
2299-
LangOpts.getStackProtector() == LangOptions::SSPOn)
2307+
isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPOn))
23002308
B.addAttribute(llvm::Attribute::StackProtectStrong);
2301-
else if (LangOpts.getStackProtector() == LangOptions::SSPOn)
2309+
else if (isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPOn))
23022310
B.addAttribute(llvm::Attribute::StackProtect);
2303-
else if (LangOpts.getStackProtector() == LangOptions::SSPStrong)
2311+
else if (isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPStrong))
23042312
B.addAttribute(llvm::Attribute::StackProtectStrong);
2305-
else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
2313+
else if (isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPReq))
23062314
B.addAttribute(llvm::Attribute::StackProtectReq);
23072315

23082316
if (!D) {

0 commit comments

Comments
 (0)