Skip to content

Commit 0c6079a

Browse files
committed
[DirectX] Use an allow-list on module metadata
1 parent c431ee7 commit 0c6079a

File tree

2 files changed

+97
-13
lines changed

2 files changed

+97
-13
lines changed

llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,16 @@ static void cleanModuleFlags(Module &M) {
364364
M.addModuleFlag(Flag.Behavior, Flag.Key->getString(), Flag.Val);
365365
}
366366

367+
using GlobalMDList = std::array<StringLiteral, 7>;
368+
369+
// The following are compatible with DXIL but not emit with clang, they can
370+
// be added when applicable:
371+
// dx.typeAnnotations, dx.viewIDState, dx.dxrPayloadAnnotations
372+
static GlobalMDList CompatibleNamedModuleMDs = {
373+
"llvm.ident", "llvm.module.flags", "dx.resources", "dx.valver",
374+
"dx.shaderModel", "dx.version", "dx.entryPoints",
375+
};
376+
367377
static void translateGlobalMetadata(Module &M, DXILResourceMap &DRM,
368378
DXILResourceTypeMap &DRTM,
369379
const ModuleShaderFlags &ShaderFlags,
@@ -426,19 +436,18 @@ static void translateGlobalMetadata(Module &M, DXILResourceMap &DRM,
426436

427437
cleanModuleFlags(M);
428438

429-
// dx.rootsignatures will have been parsed from its metadata form as its
430-
// binary form as part of the RootSignatureAnalysisWrapper, so safely
431-
// remove it as it is not recognized in DXIL
432-
if (NamedMDNode *RootSignature = M.getNamedMetadata("dx.rootsignatures"))
433-
RootSignature->eraseFromParent();
434-
435-
// llvm.errno.tbaa was recently added but is not supported in LLVM 3.7 and
436-
// causes all tests using the DXIL Validator to fail.
437-
//
438-
// This is a temporary fix and should be replaced with a allowlist once
439-
// we have determined all metadata that the DXIL Validator allows
440-
if (NamedMDNode *ErrNo = M.getNamedMetadata("llvm.errno.tbaa"))
441-
ErrNo->eraseFromParent();
439+
// Finally, strip all module metadata that is not explicitly specified in the
440+
// allow-list
441+
SmallVector<NamedMDNode *> ToStrip;
442+
443+
for (NamedMDNode &NamedMD : M.named_metadata()) {
444+
if (!NamedMD.getName().starts_with("llvm.dbg.") &&
445+
!llvm::is_contained(CompatibleNamedModuleMDs, NamedMD.getName()))
446+
ToStrip.push_back(&NamedMD);
447+
}
448+
449+
for (NamedMDNode *NamedMD : ToStrip)
450+
NamedMD->eraseFromParent();
442451
}
443452

444453
PreservedAnalyses DXILTranslateMetadata::run(Module &M,
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
; RUN: opt -S -dxil-translate-metadata < %s | FileCheck %s
2+
3+
; Ensures that only metadata explictly specified on the allow list, or debug
4+
; related, metadata is emitted
5+
6+
target triple = "dxil-unknown-shadermodel6.0-compute"
7+
8+
; CHECK-NOT: !dx.rootsignatures
9+
; CHECK-NOT: !llvm.errno.tbaa
10+
11+
; CHECK-DAG: !llvm.dbg.cu
12+
13+
; CHECK-DAG: !llvm.module.flags = !{![[#DWARF_VER:]], ![[#DEBUG_VER:]]}
14+
; CHECK-DAG: !llvm.ident = !{![[#IDENT:]]}
15+
16+
; CHECK-DAG: !dx.shaderModel
17+
; CHECK-DAG: !dx.version
18+
; CHECK-DAG: !dx.entryPoints
19+
; CHECK-DAG: !dx.valver
20+
; CHECK-DAG: !dx.resources
21+
22+
; CHECK-NOT: !dx.rootsignatures
23+
; CHECK-NOT: !llvm.errno.tbaa
24+
25+
; Check allowed llvm metadata structure to ensure it is still DXIL compatible
26+
; If this fails, please ensure that the updated form is DXIL compatible before
27+
; updating the test.
28+
29+
; CHECK-DAG: ![[#IDENT]] = !{!"clang 22.0.0"}
30+
; CHECK-DAG: ![[#DWARF_VER]] = !{i32 2, !"Dwarf Version", i32 2}
31+
; CHECK-DAG: ![[#DEBUG_VER]] = !{i32 2, !"Debug Info Version", i32 3}
32+
33+
; CHECK-NOT: !dx.rootsignatures
34+
; CHECK-NOT: !llvm.errno.tbaa
35+
36+
@BufA.str = private unnamed_addr constant [5 x i8] c"BufA\00", align 1
37+
38+
define void @main () #0 {
39+
entry:
40+
%typed0 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
41+
@llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_v4f32_1_0_0(
42+
i32 3, i32 5, i32 1, i32 0, ptr @BufA.str)
43+
ret void
44+
}
45+
46+
attributes #0 = { noinline nounwind "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
47+
48+
; Incompatible
49+
!dx.rootsignatures = !{!2}
50+
!llvm.errno.tbaa = !{!5}
51+
52+
; Compatible
53+
!llvm.dbg.cu = !{!8}
54+
!llvm.module.flags = !{!11, !12}
55+
!llvm.ident = !{!13}
56+
!dx.valver = !{!14}
57+
58+
!2 = !{ ptr @main, !3, i32 2 }
59+
!3 = !{ !4 }
60+
!4 = !{ !"RootFlags", i32 1 }
61+
62+
!5 = !{!6, !6, i64 0}
63+
!6 = !{!"omnipotent char", !7}
64+
!7 = !{!"Simple C/C++ TBAA"}
65+
66+
!8 = distinct !DICompileUnit(language: DW_LANG_C99, file: !9, producer: "Some Compiler", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !10, splitDebugInlining: false, nameTableKind: None)
67+
!9 = !DIFile(filename: "hlsl.hlsl", directory: "/some-path")
68+
!10 = !{}
69+
70+
!11 = !{i32 2, !"Dwarf Version", i32 2}
71+
!12 = !{i32 2, !"Debug Info Version", i32 3}
72+
73+
!13 = !{!"clang 22.0.0"}
74+
75+
!14 = !{i32 1, i32 1}

0 commit comments

Comments
 (0)