Skip to content

Commit c538d5c

Browse files
[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.
1 parent 3e79c7f commit c538d5c

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ bool isConvergenceIntrinsic(const Instruction *I) {
191191
II->getIntrinsicID() == Intrinsic::experimental_convergence_anchor;
192192
}
193193

194+
bool expectIgnoredInIRTranslation(const Instruction *I) {
195+
const auto *II = dyn_cast<IntrinsicInst>(I);
196+
if (!II)
197+
return false;
198+
return II->getIntrinsicID() == Intrinsic::invariant_start;
199+
}
200+
194201
bool allowEmitFakeUse(const Value *Arg) {
195202
if (const auto *II = dyn_cast<IntrinsicInst>(Arg))
196203
if (Function *F = II->getCalledFunction())
@@ -1567,7 +1574,8 @@ void SPIRVEmitIntrinsics::processInstrAfterVisit(Instruction *I,
15671574
I->setOperand(OpNo, NewOp);
15681575
}
15691576
}
1570-
if (I->hasName() && !I->getType()->isAggregateType()) {
1577+
if (I->hasName() && !I->getType()->isAggregateType() &&
1578+
!expectIgnoredInIRTranslation(I)) {
15711579
reportFatalOnTokenType(I);
15721580
setInsertPointAfterDef(B, I);
15731581
std::vector<Value *> Args = {I};

llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,15 @@ bool SPIRVInstructionSelector::spvSelect(Register ResVReg,
754754
case TargetOpcode::G_UNMERGE_VALUES:
755755
return selectUnmergeValues(I);
756756

757+
// Discard gen opcodes for intrinsics which we do not expect to actually
758+
// represent code after lowering or intrinsics which are not implemented but
759+
// should not crash when found in a customer's LLVM IR input.
760+
case TargetOpcode::G_TRAP:
761+
case TargetOpcode::G_DEBUGTRAP:
762+
case TargetOpcode::G_UBSANTRAP:
763+
case TargetOpcode::DBG_LABEL:
764+
return true;
765+
757766
default:
758767
return false;
759768
}
@@ -2636,8 +2645,15 @@ bool SPIRVInstructionSelector::selectIntrinsic(Register ResVReg,
26362645
}
26372646
case Intrinsic::spv_step:
26382647
return selectStep(ResVReg, ResType, I);
2648+
// Discard intrinsics which we do not expect to actually represent code after
2649+
// lowering or intrinsics which are not implemented but should not crash when
2650+
// found in a customer's LLVM IR input.
2651+
case Intrinsic::instrprof_increment:
2652+
case Intrinsic::instrprof_increment_step:
2653+
case Intrinsic::instrprof_value_profile:
2654+
break;
2655+
// Discard internal intrinsics.
26392656
case Intrinsic::spv_value_md:
2640-
// ignore the intrinsic
26412657
break;
26422658
default: {
26432659
std::string DiagMsg;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
2+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
3+
4+
; Ensure that these calls do not represent any code and don't cause a crash.
5+
; CHECK: OpFunction
6+
; CHECK-NEXT: OpFunctionParameter
7+
; CHECK-NEXT: OpLabel
8+
; CHECK-NEXT: OpReturn
9+
; CHECK-NEXT: OpFunctionEnd
10+
11+
define spir_kernel void @foo(ptr %p) {
12+
entry:
13+
call void @llvm.trap()
14+
call void @llvm.debugtrap()
15+
call void @llvm.ubsantrap(i8 100)
16+
17+
%r1 = call ptr @llvm.invariant.start.p0(i64 1024, ptr %p)
18+
call void @llvm.invariant.end.p0(ptr %r1, i64 1024, ptr %p)
19+
20+
call void @llvm.instrprof.increment(ptr %p, i64 0, i32 1, i32 0)
21+
call void @llvm.instrprof.increment.step(ptr %p, i64 0, i32 1, i32 0, i64 1)
22+
call void @llvm.instrprof.value.profile(ptr %p, i64 0, i64 0, i32 1, i32 0)
23+
24+
ret void
25+
}

0 commit comments

Comments
 (0)