Skip to content

Commit 0be3f13

Browse files
authored
[DirectX] only allow intrinsics defined in DXIL.td (#128613)
Fixes #128071 The current behavior lets intrinsics that don't map to a DXILOP slip through. Nothing catches this until we hit the DXIL validator. This change fails earlier so we don't encode invalid llvm intrinsics that can slip through because of clang builtins like `__builtin_reduce_and` example: https://hlsl.godbolt.org/z/13rPj18vn
1 parent 303d7fa commit 0be3f13

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

llvm/lib/Target/DirectX/DXILOpLowering.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,8 +770,20 @@ class OpLowerer {
770770
continue;
771771
Intrinsic::ID ID = F.getIntrinsicID();
772772
switch (ID) {
773-
default:
773+
// NOTE: Skip dx_resource_casthandle here. They are
774+
// resolved after this loop in cleanupHandleCasts.
775+
case Intrinsic::dx_resource_casthandle:
776+
// NOTE: llvm.dbg.value is supported as is in DXIL.
777+
case Intrinsic::dbg_value:
778+
case Intrinsic::not_intrinsic:
774779
continue;
780+
default: {
781+
DiagnosticInfoUnsupported Diag(
782+
F, "Unsupported intrinsic for DXIL lowering");
783+
M.getContext().diagnose(Diag);
784+
HasErrors |= true;
785+
break;
786+
}
775787
#define DXIL_OP_INTRINSIC(OpCode, Intrin, ...) \
776788
case Intrin: \
777789
HasErrors |= replaceFunctionWithOp( \

llvm/test/CodeGen/DirectX/clamp.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ declare <3 x half> @llvm.dx.nclamp.v3f16(<3 x half>, <3 x half>, <3 x half>)
286286
declare <4 x float> @llvm.dx.nclamp.v4f32(<4 x float>, <4 x float>, <4 x float>)
287287
declare <2 x double> @llvm.dx.nclamp.v2f64(<2 x double>, <2 x double>, <2 x double>)
288288
declare <4 x i32> @llvm.dx.sclamp.v4i32(<4 x i32>, <4 x i32>, <4 x i32>)
289-
declare <3 x i16> @llvm.dx.uclamp.v3i32(<3 x i16>, <3 x i32>, <3 x i16>)
289+
declare <3 x i16> @llvm.dx.uclamp.v3i16(<3 x i16>, <3 x i16>, <3 x i16>)
290290
declare <4 x i32> @llvm.dx.uclamp.v4i32(<4 x i32>, <4 x i32>, <4 x i32>)
291291
declare <2 x i64> @llvm.dx.uclamp.v2i64(<2 x i64>, <2 x i64>, <2 x i64>)
292292

llvm/test/CodeGen/DirectX/discard.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: opt -passes='function(scalarizer),module(dxil-op-lower,dxil-intrinsic-expansion)' -S -mtriple=dxil-pc-shadermodel6.3-pixel %s | FileCheck %s
1+
; RUN: opt -passes='function(scalarizer),module(dxil-intrinsic-expansion,dxil-op-lower)' -S -mtriple=dxil-pc-shadermodel6.3-pixel %s | FileCheck %s
22

33
; CHECK-LABEL: define void @test_scalar
44
; CHECK: call void @dx.op.discard(i32 82, i1 %0)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; RUN: not opt -S -dxil-op-lower -mtriple=dxil-pc-shadermodel6.3-library %s 2>&1 | FileCheck %s
2+
3+
; CHECK: error: <unknown>:0:0: in function llvm.vector.reduce.and.v4i32 i32 (<4 x i32>): Unsupported intrinsic for DXIL lowering
4+
define i32 @fn_and(<4 x i32> %0) local_unnamed_addr #0 {
5+
%2 = tail call i32 @llvm.vector.reduce.and.v4i32(<4 x i32> %0)
6+
ret i32 %2
7+
}
8+
9+
declare i32 @llvm.vector.reduce.and.v4i32(<4 x i32>)
10+
11+
attributes #0 = { convergent norecurse nounwind "hlsl.export"}

0 commit comments

Comments
 (0)