Skip to content

[DirectX] fix illegal behavior flag in module flags. #96577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions llvm/lib/Target/DirectX/DXILPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,31 @@ static void removeStringFunctionAttributes(Function &F,
F.removeRetAttrs(DeadAttrs);
}

static void cleanModuleFlags(Module &M) {
NamedMDNode *MDFlags = M.getModuleFlagsMetadata();
if (!MDFlags)
return;

SmallVector<llvm::Module::ModuleFlagEntry> FlagEntries;
M.getModuleFlagsMetadata(FlagEntries);
bool Updated = false;
for (auto &Flag : FlagEntries) {
// llvm 3.7 only supports behavior up to AppendUnique.
if (Flag.Behavior <= Module::ModFlagBehavior::AppendUnique)
continue;
Flag.Behavior = Module::ModFlagBehavior::Warning;
Updated = true;
}

if (!Updated)
return;

MDFlags->eraseFromParent();

for (auto &Flag : FlagEntries)
M.addModuleFlag(Flag.Behavior, Flag.Key->getString(), Flag.Val);
}

class DXILPrepareModule : public ModulePass {

static Value *maybeGenerateBitcast(IRBuilder<> &Builder,
Expand Down Expand Up @@ -213,6 +238,8 @@ class DXILPrepareModule : public ModulePass {
}
}
}
// Remove flags not for DXIL.
cleanModuleFlags(M);
return true;
}

Expand Down
20 changes: 20 additions & 0 deletions llvm/test/CodeGen/DirectX/legalize-module-flags.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
; RUN: opt -S -dxil-prepare < %s | FileCheck %s

; Make sure behavior flag > 6 is fixed.
; CHECK: !{i32 2, !"frame-pointer", i32 2}
; CHECK: !{i32 2, !"Dwarf Version", i32 4}
; CHECK: !{i32 2, !"Debug Info Version", i32 3}

; Function Attrs: nounwind memory(none)
define void @main() local_unnamed_addr #0 {
entry:
ret void
}

attributes #0 = { nounwind memory(none) }
!llvm.module.flags = !{!0, !1, !2, !3}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"frame-pointer", i32 2}
!2 = !{i32 7, !"Dwarf Version", i32 4}
!3 = !{i32 2, !"Debug Info Version", i32 3}
17 changes: 17 additions & 0 deletions llvm/test/CodeGen/DirectX/legalize-module-flags2.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
; RUN: opt -S -dxil-prepare < %s | FileCheck %s

; CHECK: define void @main()
; Make sure behavior flag > 6 is fixed.
; CHECK:{i32 2, !"frame-pointer", i32 2}

; Function Attrs: nounwind memory(none)
define void @main() local_unnamed_addr #0 {
entry:
ret void
}

attributes #0 = { nounwind memory(none) }
!llvm.module.flags = !{!0, !1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"frame-pointer", i32 2}
4 changes: 2 additions & 2 deletions llvm/test/tools/dxil-dis/di-compile-unit.ll
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ target triple = "dxil-unknown-shadermodel6.7-library"
; CHECK: !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "Some Compiler", isOptimized: true, runtimeVersion: 0, emissionKind: 1, enums: !2)
; CHECK: !1 = !DIFile(filename: "di-compile-unit.src", directory: "/some-path")
; CHECK: !2 = !{}
; CHECK: !3 = !{i32 7, !"Dwarf Version", i32 2}
; CHECK: !4 = !{i32 2, !"Debug Info Version", i32 3}
; CHECK: !{i32 2, !"Dwarf Version", i32 2}
; CHECK: !{i32 2, !"Debug Info Version", i32 3}
4 changes: 2 additions & 2 deletions llvm/test/tools/dxil-dis/di-subprogram.ll
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ target triple = "dxil-unknown-shadermodel6.7-library"
!1 = !DIFile(filename: "some-source", directory: "some-path")
!2 = !{}

; CHECK: !3 = !{i32 7, !"Dwarf Version", i32 2}
!3 = !{i32 7, !"Dwarf Version", i32 2}
; CHECK: !3 = !{i32 2, !"Dwarf Version", i32 4}
!3 = !{i32 2, !"Dwarf Version", i32 4}
; CHECK: !4 = !{i32 2, !"Debug Info Version", i32 3}
!4 = !{i32 2, !"Debug Info Version", i32 3}

Expand Down
4 changes: 2 additions & 2 deletions llvm/test/tools/dxil-dis/di-subrange.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ target triple = "dxil-unknown-shadermodel6.7-library"
!llvm.module.flags = !{!0, !1}
!llvm.dbg.cu = !{!2}

!0 = !{i32 7, !"Dwarf Version", i32 2}
!0 = !{i32 2, !"Dwarf Version", i32 4}
!1 = !{i32 2, !"Debug Info Version", i32 3}
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "xyz", isOptimized: true, runtimeVersion: 0, emissionKind: 1, retainedTypes: !4)
!3 = !DIFile(filename: "input.hlsl", directory: "/some/path")
Expand All @@ -17,7 +17,7 @@ target triple = "dxil-unknown-shadermodel6.7-library"
; CHECK: !llvm.module.flags = !{!0, !1}
; CHECK: !llvm.dbg.cu = !{!2}

; CHECK: !0 = !{i32 7, !"Dwarf Version", i32 2}
; CHECK: !0 = !{i32 2, !"Dwarf Version", i32 4}
; CHECK: !1 = !{i32 2, !"Debug Info Version", i32 3}
; CHECK: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "xyz", isOptimized: true, runtimeVersion: 0, emissionKind: 1, retainedTypes: !4)
; CHECK: !3 = !DIFile(filename: "input.hlsl", directory: "/some/path")
Expand Down
Loading