[llvm] [SPIR-V] Fix SPIRVEmitIntrinsics undefined behavior (PR #123625)
Michal Paszkowski via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 20 06:50:35 PST 2025
https://github.com/michalpaszkowski created https://github.com/llvm/llvm-project/pull/123625
Before this change InstrSet in SPIRVEmitIntrinsics was uninitialized before running runOnFunction. This change adds a new function getPreferredInstructionSet in SPIRVSubtarget.
>From a356b104df70182cbd0e9c622faa3362a883d553 Mon Sep 17 00:00:00 2001
From: Michal Paszkowski <michal at michalpaszkowski.com>
Date: Mon, 20 Jan 2025 15:43:29 +0100
Subject: [PATCH] [SPIR-V] Fix SPIRVEmitIntrinsics undefined behavior
InstrSet in SPIRVEmitIntrinsics is uninitialized before running
runOnFunction. This change adds a new function
getPreferredInstructionSet in SPIRVSubtarget.
---
llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp | 14 +++-----------
llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp | 13 ++++++-------
llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp | 8 ++++++++
llvm/lib/Target/SPIRV/SPIRVSubtarget.h | 1 +
4 files changed, 18 insertions(+), 18 deletions(-)
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