[llvm] c538d5c - [SPIR-V] Discard some llvm intrinsics which we do not expect to actually represent code after lowering (#110233)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 1 01:48:16 PDT 2024
Author: Vyacheslav Levytskyy
Date: 2024-10-01T10:48:10+02:00
New Revision: c538d5c8b2f1236f3bbba40c1abd15cf270550a5
URL: https://github.com/llvm/llvm-project/commit/c538d5c8b2f1236f3bbba40c1abd15cf270550a5
DIFF: https://github.com/llvm/llvm-project/commit/c538d5c8b2f1236f3bbba40c1abd15cf270550a5.diff
LOG: [SPIR-V] Discard some llvm intrinsics which we do not expect to actually represent code after lowering (#110233)
There are llvm intrinsics which we do not expect to actually represent
code after lowering or which are not implemented yet but can be found in
a customer's LLVM IR input. We do not want translation to crash when
these llvm intrinsics are found, and this PR fixes the issue with
translation crash for some known cases, aligned with Khronos Translator.
Added:
llvm/test/CodeGen/SPIRV/llvm-intrinsics/ignore-llvm-intrinsic.ll
Modified:
llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
index 5e9b1358af17a1..370df24bc7af9e 100644
--- a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
@@ -191,6 +191,13 @@ bool isConvergenceIntrinsic(const Instruction *I) {
II->getIntrinsicID() == Intrinsic::experimental_convergence_anchor;
}
+bool expectIgnoredInIRTranslation(const Instruction *I) {
+ const auto *II = dyn_cast<IntrinsicInst>(I);
+ if (!II)
+ return false;
+ return II->getIntrinsicID() == Intrinsic::invariant_start;
+}
+
bool allowEmitFakeUse(const Value *Arg) {
if (const auto *II = dyn_cast<IntrinsicInst>(Arg))
if (Function *F = II->getCalledFunction())
@@ -1567,7 +1574,8 @@ void SPIRVEmitIntrinsics::processInstrAfterVisit(Instruction *I,
I->setOperand(OpNo, NewOp);
}
}
- if (I->hasName() && !I->getType()->isAggregateType()) {
+ if (I->hasName() && !I->getType()->isAggregateType() &&
+ !expectIgnoredInIRTranslation(I)) {
reportFatalOnTokenType(I);
setInsertPointAfterDef(B, I);
std::vector<Value *> Args = {I};
diff --git a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
index 60d60bc69cf5a4..c5ef59c15b730c 100644
--- a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
@@ -754,6 +754,15 @@ bool SPIRVInstructionSelector::spvSelect(Register ResVReg,
case TargetOpcode::G_UNMERGE_VALUES:
return selectUnmergeValues(I);
+ // Discard gen opcodes for intrinsics which we do not expect to actually
+ // represent code after lowering or intrinsics which are not implemented but
+ // should not crash when found in a customer's LLVM IR input.
+ case TargetOpcode::G_TRAP:
+ case TargetOpcode::G_DEBUGTRAP:
+ case TargetOpcode::G_UBSANTRAP:
+ case TargetOpcode::DBG_LABEL:
+ return true;
+
default:
return false;
}
@@ -2636,8 +2645,15 @@ bool SPIRVInstructionSelector::selectIntrinsic(Register ResVReg,
}
case Intrinsic::spv_step:
return selectStep(ResVReg, ResType, I);
+ // Discard intrinsics which we do not expect to actually represent code after
+ // lowering or intrinsics which are not implemented but should not crash when
+ // found in a customer's LLVM IR input.
+ case Intrinsic::instrprof_increment:
+ case Intrinsic::instrprof_increment_step:
+ case Intrinsic::instrprof_value_profile:
+ break;
+ // Discard internal intrinsics.
case Intrinsic::spv_value_md:
- // ignore the intrinsic
break;
default: {
std::string DiagMsg;
diff --git a/llvm/test/CodeGen/SPIRV/llvm-intrinsics/ignore-llvm-intrinsic.ll b/llvm/test/CodeGen/SPIRV/llvm-intrinsics/ignore-llvm-intrinsic.ll
new file mode 100644
index 00000000000000..a15a80754cd605
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/llvm-intrinsics/ignore-llvm-intrinsic.ll
@@ -0,0 +1,25 @@
+; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+
+; Ensure that these calls do not represent any code and don't cause a crash.
+; CHECK: OpFunction
+; CHECK-NEXT: OpFunctionParameter
+; CHECK-NEXT: OpLabel
+; CHECK-NEXT: OpReturn
+; CHECK-NEXT: OpFunctionEnd
+
+define spir_kernel void @foo(ptr %p) {
+entry:
+ call void @llvm.trap()
+ call void @llvm.debugtrap()
+ call void @llvm.ubsantrap(i8 100)
+
+ %r1 = call ptr @llvm.invariant.start.p0(i64 1024, ptr %p)
+ call void @llvm.invariant.end.p0(ptr %r1, i64 1024, ptr %p)
+
+ call void @llvm.instrprof.increment(ptr %p, i64 0, i32 1, i32 0)
+ call void @llvm.instrprof.increment.step(ptr %p, i64 0, i32 1, i32 0, i64 1)
+ call void @llvm.instrprof.value.profile(ptr %p, i64 0, i64 0, i32 1, i32 0)
+
+ ret void
+}
More information about the llvm-commits
mailing list