Skip to content

Commit ad29838

Browse files
authored
[DirectX] Use an allow-list of DXIL compatible module metadata (#165290)
This pr introduces an allow-list for module metadata, this encompasses the llvm metadata nodes: `llvm.ident` and `llvm.module.flags`, as well as, the generated `dx.` options. Resolves: #164473.
1 parent 34c58c8 commit ad29838

File tree

5 files changed

+97
-63
lines changed

5 files changed

+97
-63
lines changed

llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,16 @@ static void cleanModuleFlags(Module &M) {
472472
M.addModuleFlag(Flag.Behavior, Flag.Key->getString(), Flag.Val);
473473
}
474474

475+
using GlobalMDList = std::array<StringLiteral, 7>;
476+
477+
// The following are compatible with DXIL but not emit with clang, they can
478+
// be added when applicable:
479+
// dx.typeAnnotations, dx.viewIDState, dx.dxrPayloadAnnotations
480+
static GlobalMDList CompatibleNamedModuleMDs = {
481+
"llvm.ident", "llvm.module.flags", "dx.resources", "dx.valver",
482+
"dx.shaderModel", "dx.version", "dx.entryPoints",
483+
};
484+
475485
static void translateGlobalMetadata(Module &M, DXILResourceMap &DRM,
476486
DXILResourceTypeMap &DRTM,
477487
const ModuleShaderFlags &ShaderFlags,
@@ -526,19 +536,17 @@ static void translateGlobalMetadata(Module &M, DXILResourceMap &DRM,
526536

527537
cleanModuleFlags(M);
528538

529-
// dx.rootsignatures will have been parsed from its metadata form as its
530-
// binary form as part of the RootSignatureAnalysisWrapper, so safely
531-
// remove it as it is not recognized in DXIL
532-
if (NamedMDNode *RootSignature = M.getNamedMetadata("dx.rootsignatures"))
533-
RootSignature->eraseFromParent();
539+
// Finally, strip all module metadata that is not explicitly specified in the
540+
// allow-list
541+
SmallVector<NamedMDNode *> ToStrip;
534542

535-
// llvm.errno.tbaa was recently added but is not supported in LLVM 3.7 and
536-
// causes all tests using the DXIL Validator to fail.
537-
//
538-
// This is a temporary fix and should be replaced with a allowlist once
539-
// we have determined all metadata that the DXIL Validator allows
540-
if (NamedMDNode *ErrNo = M.getNamedMetadata("llvm.errno.tbaa"))
541-
ErrNo->eraseFromParent();
543+
for (NamedMDNode &NamedMD : M.named_metadata())
544+
if (!NamedMD.getName().starts_with("llvm.dbg.") &&
545+
!llvm::is_contained(CompatibleNamedModuleMDs, NamedMD.getName()))
546+
ToStrip.push_back(&NamedMD);
547+
548+
for (NamedMDNode *NamedMD : ToStrip)
549+
NamedMD->eraseFromParent();
542550
}
543551

544552
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}

llvm/test/tools/dxil-dis/di-subprogram.ll

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ target triple = "dxil-unknown-shadermodel6.7-library"
33

44
!llvm.dbg.cu = !{!0}
55
!llvm.module.flags = !{!3, !4}
6-
!llvm.used = !{!5}
7-
!llvm.lines = !{!13, !14, !15, !16}
86

97
; CHECK: !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "Some Compiler", isOptimized: true, runtimeVersion: 0, emissionKind: 1, enums: !2)
108
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "Some Compiler", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
@@ -16,38 +14,3 @@ target triple = "dxil-unknown-shadermodel6.7-library"
1614
!3 = !{i32 2, !"Dwarf Version", i32 4}
1715
; CHECK: !4 = !{i32 2, !"Debug Info Version", i32 3}
1816
!4 = !{i32 2, !"Debug Info Version", i32 3}
19-
20-
; CHECK: !5 = distinct !DISubprogram(name: "fma", scope: !1, file: !1, line: 1, type: !6, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: true, function: !0, variables: !9)
21-
!5 = distinct !DISubprogram(name: "fma", scope: !1, file: !1, line: 1, type: !6, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !9)
22-
23-
; CHECK: !6 = !DISubroutineType(types: !7)
24-
!6 = !DISubroutineType(types: !7)
25-
26-
; CHECK: !7 = !{!8, !8, !8, !8}
27-
!7 = !{!8, !8, !8, !8}
28-
29-
; CHECK: !8 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float)
30-
!8 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float)
31-
32-
; CHECK: !9 = !{!10, !11, !12}
33-
!9 = !{!10, !11, !12}
34-
35-
; CHECK: !10 = !DILocalVariable(tag: DW_TAG_variable, name: "x", arg: 1, scope: !5, file: !1, line: 1, type: !8)
36-
!10 = !DILocalVariable(name: "x", arg: 1, scope: !5, file: !1, line: 1, type: !8)
37-
38-
; CHECK: !11 = !DILocalVariable(tag: DW_TAG_variable, name: "y", arg: 2, scope: !5, file: !1, line: 1, type: !8)
39-
!11 = !DILocalVariable(name: "y", arg: 2, scope: !5, file: !1, line: 1, type: !8)
40-
41-
; CHECK: !12 = !DILocalVariable(tag: DW_TAG_variable, name: "z", arg: 3, scope: !5, file: !1, line: 1, type: !8)
42-
!12 = !DILocalVariable(name: "z", arg: 3, scope: !5, file: !1, line: 1, type: !8)
43-
44-
45-
; CHECK: !13 = !DILocation(line: 0, scope: !5)
46-
; CHECK: !14 = !DILocation(line: 2, column: 12, scope: !5)
47-
; CHECK: !15 = !DILocation(line: 2, column: 16, scope: !5)
48-
; CHECK: !16 = !DILocation(line: 2, column: 3, scope: !5)
49-
50-
!13 = !DILocation(line: 0, scope: !5)
51-
!14 = !DILocation(line: 2, column: 12, scope: !5)
52-
!15 = !DILocation(line: 2, column: 16, scope: !5)
53-
!16 = !DILocation(line: 2, column: 3, scope: !5)

llvm/test/tools/dxil-dis/di-subrotine.ll

Lines changed: 0 additions & 12 deletions
This file was deleted.

llvm/test/tools/dxil-dis/md-manystrings.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
target triple = "dxil-unknown-shadermodel6.7-library"
66

7-
!llvm.too_many_strings = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10, !11, !12, !13, !14, !15, !16, !17, !18, !19, !20, !21, !22, !23, !24, !25, !26, !27, !28, !29, !30, !31}
7+
!llvm.ident = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10, !11, !12, !13, !14, !15, !16, !17, !18, !19, !20, !21, !22, !23, !24, !25, !26, !27, !28, !29, !30, !31}
88

99
!0 = !{!"String 0"}
1010
!1 = !{!"String 1"}
@@ -39,7 +39,7 @@ target triple = "dxil-unknown-shadermodel6.7-library"
3939
!30 = !{!"String 30"}
4040
!31 = !{!"String 31"}
4141

42-
; CHECK: !llvm.too_many_strings = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10, !11, !12, !13, !14, !15, !16, !17, !18, !19, !20, !21, !22, !23, !24, !25, !26, !27, !28, !29, !30, !31}
42+
; CHECK: !llvm.ident = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10, !11, !12, !13, !14, !15, !16, !17, !18, !19, !20, !21, !22, !23, !24, !25, !26, !27, !28, !29, !30, !31}
4343
; CHECK: !0 = !{!"String 0"}
4444
; CHECK: !1 = !{!"String 1"}
4545
; CHECK: !2 = !{!"String 2"}

0 commit comments

Comments
 (0)