[llvm] 5810f15 - [SPIR-V] Fix SPIRVEmitIntrinsics undefined behavior (#123625)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 20 09:23:10 PST 2025
Author: Michal Paszkowski
Date: 2025-01-20T09:23:07-08:00
New Revision: 5810f157cd048fd7e2fc20f4f782462164279eba
URL: https://github.com/llvm/llvm-project/commit/5810f157cd048fd7e2fc20f4f782462164279eba
DIFF: https://github.com/llvm/llvm-project/commit/5810f157cd048fd7e2fc20f4f782462164279eba.diff
LOG: [SPIR-V] Fix SPIRVEmitIntrinsics undefined behavior (#123625)
Before this change InstrSet in SPIRVEmitIntrinsics was uninitialized
before running runOnFunction. This change adds a new function
getPreferredInstructionSet in SPIRVSubtarget.
Added:
Modified:
llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp
llvm/lib/Target/SPIRV/SPIRVSubtarget.h
Removed:
################################################################################
diff --git a/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp b/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
index 44b6f5f8d507be..78f6b188c45c15 100644
--- a/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
@@ -544,13 +544,7 @@ bool SPIRVCallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
const auto *ST = static_cast<const SPIRVSubtarget *>(&MF.getSubtarget());
bool isFunctionDecl = CF && CF->isDeclaration();
- bool canUseOpenCL = ST->canUseExtInstSet(SPIRV::InstructionSet::OpenCL_std);
- bool canUseGLSL = ST->canUseExtInstSet(SPIRV::InstructionSet::GLSL_std_450);
- assert(canUseGLSL != canUseOpenCL &&
- "Scenario where both sets are enabled is not supported.");
-
- if (isFunctionDecl && !DemangledName.empty() &&
- (canUseGLSL || canUseOpenCL)) {
+ if (isFunctionDecl && !DemangledName.empty()) {
if (ResVReg.isValid()) {
if (!GR->getSPIRVTypeForVReg(ResVReg)) {
const Type *RetTy = OrigRetTy;
@@ -607,11 +601,9 @@ bool SPIRVCallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
GR->getPointerSize()));
}
}
- auto instructionSet = canUseOpenCL ? SPIRV::InstructionSet::OpenCL_std
- : SPIRV::InstructionSet::GLSL_std_450;
if (auto Res =
- SPIRV::lowerBuiltin(DemangledName, instructionSet, MIRBuilder,
- ResVReg, OrigRetTy, ArgVRegs, GR))
+ SPIRV::lowerBuiltin(DemangledName, ST->getPreferredInstructionSet(),
+ MIRBuilder, ResVReg, OrigRetTy, ArgVRegs, GR))
return *Res;
}
diff --git a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
index 1c1acd29ee0e6a..702206b8e0dc56 100644
--- a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
@@ -74,7 +74,6 @@ class SPIRVEmitIntrinsics
DenseMap<Instruction *, Constant *> AggrConsts;
DenseMap<Instruction *, Type *> AggrConstTypes;
DenseSet<Instruction *> AggrStores;
- SPIRV::InstructionSet::InstructionSet InstrSet;
// map of function declarations to <pointer arg index => element type>
DenseMap<Function *, SmallVector<std::pair<unsigned, Type *>>> FDeclPtrTys;
@@ -896,8 +895,9 @@ bool SPIRVEmitIntrinsics::deduceOperandElementTypeCalledFunction(
getOclOrSpirvBuiltinDemangledName(CalledF->getName());
if (DemangledName.length() > 0 &&
!StringRef(DemangledName).starts_with("llvm.")) {
- auto [Grp, Opcode, ExtNo] =
- SPIRV::mapBuiltinToOpcode(DemangledName, InstrSet);
+ const SPIRVSubtarget &ST = TM->getSubtarget<SPIRVSubtarget>(*CalledF);
+ auto [Grp, Opcode, ExtNo] = SPIRV::mapBuiltinToOpcode(
+ DemangledName, ST.getPreferredInstructionSet());
if (Opcode == SPIRV::OpGroupAsyncCopy) {
for (unsigned i = 0, PtrCnt = 0; i < CI->arg_size() && PtrCnt < 2; ++i) {
Value *Op = CI->getArgOperand(i);
@@ -2317,8 +2317,6 @@ bool SPIRVEmitIntrinsics::runOnFunction(Function &Func) {
const SPIRVSubtarget &ST = TM->getSubtarget<SPIRVSubtarget>(Func);
GR = ST.getSPIRVGlobalRegistry();
- InstrSet = ST.isOpenCLEnv() ? SPIRV::InstructionSet::OpenCL_std
- : SPIRV::InstructionSet::GLSL_std_450;
if (!CurrF)
HaveFunPtrs =
@@ -2475,8 +2473,9 @@ void SPIRVEmitIntrinsics::parseFunDeclarations(Module &M) {
if (DemangledName.empty())
continue;
// allow only OpGroupAsyncCopy use case at the moment
- auto [Grp, Opcode, ExtNo] =
- SPIRV::mapBuiltinToOpcode(DemangledName, InstrSet);
+ const SPIRVSubtarget &ST = TM->getSubtarget<SPIRVSubtarget>(F);
+ auto [Grp, Opcode, ExtNo] = SPIRV::mapBuiltinToOpcode(
+ DemangledName, ST.getPreferredInstructionSet());
if (Opcode != SPIRV::OpGroupAsyncCopy)
continue;
// find pointer arguments
diff --git a/llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp b/llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp
index fc35a3e06c43f7..a476b51c3120af 100644
--- a/llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp
@@ -111,6 +111,14 @@ bool SPIRVSubtarget::canUseExtInstSet(
return AvailableExtInstSets.contains(E);
}
+SPIRV::InstructionSet::InstructionSet
+SPIRVSubtarget::getPreferredInstructionSet() const {
+ if (isOpenCLEnv())
+ return SPIRV::InstructionSet::OpenCL_std;
+ else
+ return SPIRV::InstructionSet::GLSL_std_450;
+}
+
bool SPIRVSubtarget::isAtLeastSPIRVVer(VersionTuple VerToCompareTo) const {
return isAtLeastVer(SPIRVVersion, VerToCompareTo);
}
diff --git a/llvm/lib/Target/SPIRV/SPIRVSubtarget.h b/llvm/lib/Target/SPIRV/SPIRVSubtarget.h
index 984ba953e874f5..e587739a7636fb 100644
--- a/llvm/lib/Target/SPIRV/SPIRVSubtarget.h
+++ b/llvm/lib/Target/SPIRV/SPIRVSubtarget.h
@@ -96,6 +96,7 @@ class SPIRVSubtarget : public SPIRVGenSubtargetInfo {
}
bool canUseExtension(SPIRV::Extension::Extension E) const;
bool canUseExtInstSet(SPIRV::InstructionSet::InstructionSet E) const;
+ SPIRV::InstructionSet::InstructionSet getPreferredInstructionSet() const;
SPIRVGlobalRegistry *getSPIRVGlobalRegistry() const { return GR.get(); }
More information about the llvm-commits
mailing list