Skip to content

Commit cbfbc4c

Browse files
committed
[DirectX] remove module flags not for DXIL.
Remove all module flags other than "Dwarf Version" and "Debug Info Version". Fixes #90773
1 parent 16903ac commit cbfbc4c

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

llvm/lib/Target/DirectX/DXILPrepare.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,30 @@ static void removeStringFunctionAttributes(Function &F,
116116
F.removeRetAttrs(DeadAttrs);
117117
}
118118

119+
static void cleanModuleFlags(Module &M) {
120+
NamedMDNode *MDFlags = M.getModuleFlagsMetadata();
121+
if (!MDFlags)
122+
return;
123+
124+
StringSet<> LiveKeys = {"Dwarf Version", "Debug Info Version"};
125+
126+
SmallVector<llvm::Module::ModuleFlagEntry> FlagEntries;
127+
M.getModuleFlagsMetadata(FlagEntries);
128+
SmallVector<llvm::Module::ModuleFlagEntry> LiveFlagEntries;
129+
for (auto &Flag : FlagEntries) {
130+
if (LiveKeys.count(Flag.Key->getString()))
131+
LiveFlagEntries.push_back(Flag);
132+
}
133+
134+
if (LiveFlagEntries.size() == FlagEntries.size())
135+
return;
136+
137+
MDFlags->eraseFromParent();
138+
139+
for (auto &Flag : LiveFlagEntries)
140+
M.addModuleFlag(Flag.Behavior, Flag.Key->getString(), Flag.Val);
141+
}
142+
119143
class DXILPrepareModule : public ModulePass {
120144

121145
static Value *maybeGenerateBitcast(IRBuilder<> &Builder,
@@ -213,6 +237,8 @@ class DXILPrepareModule : public ModulePass {
213237
}
214238
}
215239
}
240+
// Remove flags not for DXIL.
241+
cleanModuleFlags(M);
216242
return true;
217243
}
218244

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; RUN: opt -S -dxil-prepare < %s | FileCheck %s
2+
3+
; Make sure non-dxil module flags are removed.
4+
; CHECK-NOT:"wchar_size"
5+
; CHECK-NOT:"frame-pointer"
6+
7+
; CHECK:!llvm.module.flags = !{!0, !1}
8+
; CHECK:!0 = !{i32 7, !"Dwarf Version", i32 2}
9+
; CHECK:!1 = !{i32 2, !"Debug Info Version", i32 3}
10+
11+
; Function Attrs: nounwind memory(none)
12+
define void @main() local_unnamed_addr #0 {
13+
entry:
14+
ret void
15+
}
16+
17+
attributes #0 = { nounwind memory(none) }
18+
!llvm.module.flags = !{!0, !1, !2, !3}
19+
20+
!0 = !{i32 1, !"wchar_size", i32 4}
21+
!1 = !{i32 7, !"frame-pointer", i32 2}
22+
!2 = !{i32 7, !"Dwarf Version", i32 2}
23+
!3 = !{i32 2, !"Debug Info Version", i32 3}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: opt -S -dxil-prepare < %s | FileCheck %s
2+
3+
; CHECK: define void @main()
4+
; Make sure non-dxil module flags are removed.
5+
; CHECK-NOT:"wchar_size"
6+
; CHECK-NOT:"frame-pointer"
7+
8+
; Function Attrs: nounwind memory(none)
9+
define void @main() local_unnamed_addr #0 {
10+
entry:
11+
ret void
12+
}
13+
14+
attributes #0 = { nounwind memory(none) }
15+
!llvm.module.flags = !{!0, !1}
16+
17+
!0 = !{i32 1, !"wchar_size", i32 4}
18+
!1 = !{i32 7, !"frame-pointer", i32 2}

0 commit comments

Comments
 (0)