diff --git a/clang/test/CodeGen/piclevels.c b/clang/test/CodeGen/piclevels.c --- a/clang/test/CodeGen/piclevels.c +++ b/clang/test/CodeGen/piclevels.c @@ -4,9 +4,9 @@ // RUN: %clang_cc1 -emit-llvm -pic-level 1 -pic-is-pie %s -o - | FileCheck %s -check-prefix=CHECK-SMALLPIC -check-prefix=CHECK-SMALLPIE // CHECK-BIGPIC: !llvm.module.flags = !{{{.*}}} -// CHECK-BIGPIC: !{{[0-9]+}} = !{i32 7, !"PIC Level", i32 2} +// CHECK-BIGPIC: ![[#]] = !{i32 8, !"PIC Level", i32 2} // CHECK-SMALLPIC: !llvm.module.flags = !{{{.*}}} -// CHECK-SMALLPIC: !{{[0-9]+}} = !{i32 7, !"PIC Level", i32 1} +// CHECK-SMALLPIC: ![[#]] = !{i32 8, !"PIC Level", i32 1} // CHECK-NOPIE-NOT: PIE Level -// CHECK-BIGPIE: !{{[0-9]+}} = !{i32 7, !"PIE Level", i32 2} -// CHECK-SMALLPIE: !{{[0-9]+}} = !{i32 7, !"PIE Level", i32 1} +// CHECK-BIGPIE: ![[#]] = !{i32 7, !"PIE Level", i32 2} +// CHECK-SMALLPIE: ![[#]] = !{i32 7, !"PIE Level", i32 1} diff --git a/llvm/lib/IR/AutoUpgrade.cpp b/llvm/lib/IR/AutoUpgrade.cpp --- a/llvm/lib/IR/AutoUpgrade.cpp +++ b/llvm/lib/IR/AutoUpgrade.cpp @@ -4391,26 +4391,34 @@ MDString *ID = dyn_cast_or_null(Op->getOperand(1)); if (!ID) continue; + auto SetBehavior = [&](Module::ModFlagBehavior B) { + Metadata *Ops[3] = {ConstantAsMetadata::get(ConstantInt::get( + Type::getInt32Ty(M.getContext()), B)), + MDString::get(M.getContext(), ID->getString()), + Op->getOperand(2)}; + ModFlags->setOperand(I, MDNode::get(M.getContext(), Ops)); + Changed = true; + }; + if (ID->getString() == "Objective-C Image Info Version") HasObjCFlag = true; if (ID->getString() == "Objective-C Class Properties") HasClassProperties = true; - // Upgrade PIC/PIE Module Flags. The module flag behavior for these two - // field was Error and now they are Max. - if (ID->getString() == "PIC Level" || ID->getString() == "PIE Level") { + // Upgrade PIC from Error/Max to Min. + if (ID->getString() == "PIC Level") { if (auto *Behavior = mdconst::dyn_extract_or_null(Op->getOperand(0))) { - if (Behavior->getLimitedValue() == Module::Error) { - Type *Int32Ty = Type::getInt32Ty(M.getContext()); - Metadata *Ops[3] = { - ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Module::Max)), - MDString::get(M.getContext(), ID->getString()), - Op->getOperand(2)}; - ModFlags->setOperand(I, MDNode::get(M.getContext(), Ops)); - Changed = true; - } + uint64_t V = Behavior->getLimitedValue(); + if (V == Module::Error || V == Module::Max) + SetBehavior(Module::Min); } } + // Upgrade "PIE Level" from Error to Max. + if (ID->getString() == "PIE Level") + if (auto *Behavior = + mdconst::dyn_extract_or_null(Op->getOperand(0))) + if (Behavior->getLimitedValue() == Module::Error) + SetBehavior(Module::Max); // Upgrade branch protection and return address signing module flags. The // module flag behavior for these fields were Error and now they are Min. diff --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp --- a/llvm/lib/IR/Module.cpp +++ b/llvm/lib/IR/Module.cpp @@ -596,7 +596,9 @@ } void Module::setPICLevel(PICLevel::Level PL) { - addModuleFlag(ModFlagBehavior::Max, "PIC Level", PL); + // The merge result of a non-PIC object and a PIC object can only be reliably + // used as a non-PIC object, so use the Min merge behavior. + addModuleFlag(ModFlagBehavior::Min, "PIC Level", PL); } PIELevel::Level Module::getPIELevel() const { diff --git a/llvm/test/Bitcode/upgrade-module-flag.ll b/llvm/test/Bitcode/upgrade-module-flag.ll --- a/llvm/test/Bitcode/upgrade-module-flag.ll +++ b/llvm/test/Bitcode/upgrade-module-flag.ll @@ -8,7 +8,7 @@ !2 = !{i32 1, !"Objective-C Image Info Version", i32 0} !3 = !{i32 1, !"Objective-C Image Info Section", !"__DATA, __objc_imageinfo, regular, no_dead_strip"} -; CHECK: !0 = !{i32 7, !"PIC Level", i32 1} +; CHECK: !0 = !{i32 8, !"PIC Level", i32 1} ; CHECK: !1 = !{i32 7, !"PIE Level", i32 1} ; CHECK: !2 = !{i32 1, !"Objective-C Image Info Version", i32 0} ; CHECK: !3 = !{i32 1, !"Objective-C Image Info Section", !"__DATA,__objc_imageinfo,regular,no_dead_strip"} diff --git a/llvm/test/Bitcode/upgrade-pic-level.ll b/llvm/test/Bitcode/upgrade-pic-level.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Bitcode/upgrade-pic-level.ll @@ -0,0 +1,8 @@ +; RUN: llvm-as < %s | llvm-dis | FileCheck %s +; RUN: verify-uselistorder < %s + +!llvm.module.flags = !{!0} + +; CHECK: !0 = !{i32 8, !"PIC Level", i32 1} + +!0 = !{i32 7, !"PIC Level", i32 1} diff --git a/llvm/test/Linker/Inputs/module-flags-pic-2-b.ll b/llvm/test/Linker/Inputs/module-flags-pic-2-b.ll --- a/llvm/test/Linker/Inputs/module-flags-pic-2-b.ll +++ b/llvm/test/Linker/Inputs/module-flags-pic-2-b.ll @@ -1,4 +1,4 @@ -!0 = !{ i32 7, !"PIC Level", i32 2 } +!0 = !{ i32 8, !"PIC Level", i32 2 } !1 = !{ i32 7, !"PIE Level", i32 2 } !llvm.module.flags = !{!0, !1} diff --git a/llvm/test/Linker/module-flags-pic-1-a.ll b/llvm/test/Linker/module-flags-pic-1-a.ll --- a/llvm/test/Linker/module-flags-pic-1-a.ll +++ b/llvm/test/Linker/module-flags-pic-1-a.ll @@ -6,4 +6,4 @@ !llvm.module.flags = !{!0} ; CHECK: !llvm.module.flags = !{!0} -; CHECK: !0 = !{i32 7, !"PIC Level", i32 1} +; CHECK: !0 = !{i32 8, !"PIC Level", i32 1} diff --git a/llvm/test/Linker/module-flags-pic-2-a.ll b/llvm/test/Linker/module-flags-pic-2-a.ll --- a/llvm/test/Linker/module-flags-pic-2-a.ll +++ b/llvm/test/Linker/module-flags-pic-2-a.ll @@ -2,10 +2,10 @@ ; test linking modules with two different PIC and PIE levels -!0 = !{ i32 7, !"PIC Level", i32 1 } +!0 = !{ i32 8, !"PIC Level", i32 1 } !1 = !{ i32 7, !"PIE Level", i32 1 } !llvm.module.flags = !{!0, !1} -; CHECK: !0 = !{i32 7, !"PIC Level", i32 2} +; CHECK: !0 = !{i32 8, !"PIC Level", i32 1} ; CHECK: !1 = !{i32 7, !"PIE Level", i32 2} diff --git a/llvm/test/Transforms/Attributor/value-simplify-dbg.ll b/llvm/test/Transforms/Attributor/value-simplify-dbg.ll --- a/llvm/test/Transforms/Attributor/value-simplify-dbg.ll +++ b/llvm/test/Transforms/Attributor/value-simplify-dbg.ll @@ -58,7 +58,7 @@ !7 = !{i32 7, !"Dwarf Version", i32 5} !8 = !{i32 2, !"Debug Info Version", i32 3} !9 = !{i32 1, !"wchar_size", i32 4} -!10 = !{i32 7, !"PIC Level", i32 2} +!10 = !{i32 8, !"PIC Level", i32 2} !11 = !{i32 7, !"PIE Level", i32 2} !12 = !{i32 7, !"uwtable", i32 2} !13 = !{i32 7, !"frame-pointer", i32 2} @@ -88,7 +88,7 @@ ; CHECK: [[META7:![0-9]+]] = !{i32 7, !"Dwarf Version", i32 5} ; CHECK: [[META8:![0-9]+]] = !{i32 2, !"Debug Info Version", i32 3} ; CHECK: [[META9:![0-9]+]] = !{i32 1, !"wchar_size", i32 4} -; CHECK: [[META10:![0-9]+]] = !{i32 7, !"PIC Level", i32 2} +; CHECK: [[META10:![0-9]+]] = !{i32 8, !"PIC Level", i32 2} ; CHECK: [[META11:![0-9]+]] = !{i32 7, !"PIE Level", i32 2} ; CHECK: [[META12:![0-9]+]] = !{i32 7, !"uwtable", i32 2} ; CHECK: [[META13:![0-9]+]] = !{i32 7, !"frame-pointer", i32 2} diff --git a/llvm/test/Transforms/Attributor/value-simplify-local-remote.ll b/llvm/test/Transforms/Attributor/value-simplify-local-remote.ll --- a/llvm/test/Transforms/Attributor/value-simplify-local-remote.ll +++ b/llvm/test/Transforms/Attributor/value-simplify-local-remote.ll @@ -769,7 +769,7 @@ !1 = !{i32 1, !"wchar_size", i32 4} !2 = !{i32 7, !"openmp", i32 50} !3 = !{i32 7, !"openmp-device", i32 50} -!4 = !{i32 7, !"PIC Level", i32 2} +!4 = !{i32 8, !"PIC Level", i32 2} !5 = !{i32 7, !"frame-pointer", i32 2} !6 = !{i32 7, !"Dwarf Version", i32 2} !7 = !{i32 2, !"Debug Info Version", i32 3} @@ -795,7 +795,7 @@ ; CHECK: [[META1:![0-9]+]] = !{i32 1, !"wchar_size", i32 4} ; CHECK: [[META2:![0-9]+]] = !{i32 7, !"openmp", i32 50} ; CHECK: [[META3:![0-9]+]] = !{i32 7, !"openmp-device", i32 50} -; CHECK: [[META4:![0-9]+]] = !{i32 7, !"PIC Level", i32 2} +; CHECK: [[META4:![0-9]+]] = !{i32 8, !"PIC Level", i32 2} ; CHECK: [[META5:![0-9]+]] = !{i32 7, !"frame-pointer", i32 2} ; CHECK: [[META6:![0-9]+]] = !{i32 7, !"Dwarf Version", i32 2} ; CHECK: [[META7:![0-9]+]] = !{i32 2, !"Debug Info Version", i32 3} diff --git a/llvm/test/Transforms/OpenMP/spmdization.ll b/llvm/test/Transforms/OpenMP/spmdization.ll --- a/llvm/test/Transforms/OpenMP/spmdization.ll +++ b/llvm/test/Transforms/OpenMP/spmdization.ll @@ -2395,7 +2395,7 @@ !12 = !{i32 1, !"wchar_size", i32 4} !13 = !{i32 7, !"openmp", i32 50} !14 = !{i32 7, !"openmp-device", i32 50} -!15 = !{i32 7, !"PIC Level", i32 2} +!15 = !{i32 8, !"PIC Level", i32 2} !16 = !{i32 7, !"frame-pointer", i32 2} !17 = !{!"clang version 14.0.0"} !18 = !{!19, !19, i64 0} @@ -2481,7 +2481,7 @@ ; AMDGPU: [[META12:![0-9]+]] = !{i32 1, !"wchar_size", i32 4} ; AMDGPU: [[META13:![0-9]+]] = !{i32 7, !"openmp", i32 50} ; AMDGPU: [[META14:![0-9]+]] = !{i32 7, !"openmp-device", i32 50} -; AMDGPU: [[META15:![0-9]+]] = !{i32 7, !"PIC Level", i32 2} +; AMDGPU: [[META15:![0-9]+]] = !{i32 8, !"PIC Level", i32 2} ; AMDGPU: [[META16:![0-9]+]] = !{i32 7, !"frame-pointer", i32 2} ; AMDGPU: [[META17:![0-9]+]] = !{!"clang version 14.0.0"} ; AMDGPU: [[TBAA18]] = !{!19, !19, i64 0} diff --git a/llvm/test/Transforms/OpenMP/spmdization_assumes.ll b/llvm/test/Transforms/OpenMP/spmdization_assumes.ll --- a/llvm/test/Transforms/OpenMP/spmdization_assumes.ll +++ b/llvm/test/Transforms/OpenMP/spmdization_assumes.ll @@ -137,7 +137,7 @@ !2 = !{i32 1, !"wchar_size", i32 4} !3 = !{i32 7, !"openmp", i32 50} !4 = !{i32 7, !"openmp-device", i32 50} -!5 = !{i32 7, !"PIC Level", i32 2} +!5 = !{i32 8, !"PIC Level", i32 2} !6 = !{i32 7, !"frame-pointer", i32 2} !7 = !{!"clang version 14.0.0"} !8 = !{!9, !9, i64 0} @@ -159,7 +159,7 @@ ; CHECK: [[META2:![0-9]+]] = !{i32 1, !"wchar_size", i32 4} ; CHECK: [[META3:![0-9]+]] = !{i32 7, !"openmp", i32 50} ; CHECK: [[META4:![0-9]+]] = !{i32 7, !"openmp-device", i32 50} -; CHECK: [[META5:![0-9]+]] = !{i32 7, !"PIC Level", i32 2} +; CHECK: [[META5:![0-9]+]] = !{i32 8, !"PIC Level", i32 2} ; CHECK: [[META6:![0-9]+]] = !{i32 7, !"frame-pointer", i32 2} ; CHECK: [[META7:![0-9]+]] = !{!"clang version 14.0.0"} ; CHECK: [[TBAA8]] = !{!9, !9, i64 0} diff --git a/llvm/test/Transforms/OpenMP/spmdization_guarding.ll b/llvm/test/Transforms/OpenMP/spmdization_guarding.ll --- a/llvm/test/Transforms/OpenMP/spmdization_guarding.ll +++ b/llvm/test/Transforms/OpenMP/spmdization_guarding.ll @@ -381,7 +381,7 @@ !2 = !{i32 1, !"wchar_size", i32 4} !3 = !{i32 7, !"openmp", i32 50} !4 = !{i32 7, !"openmp-device", i32 50} -!5 = !{i32 7, !"PIC Level", i32 2} +!5 = !{i32 8, !"PIC Level", i32 2} !6 = !{i32 7, !"frame-pointer", i32 2} !7 = !{!"clang version 14.0.0"} !8 = !{!9} diff --git a/llvm/test/Transforms/OpenMP/spmdization_guarding_two_reaching_kernels.ll b/llvm/test/Transforms/OpenMP/spmdization_guarding_two_reaching_kernels.ll --- a/llvm/test/Transforms/OpenMP/spmdization_guarding_two_reaching_kernels.ll +++ b/llvm/test/Transforms/OpenMP/spmdization_guarding_two_reaching_kernels.ll @@ -265,7 +265,7 @@ !4 = !{i32 1, !"wchar_size", i32 4} !5 = !{i32 7, !"openmp", i32 50} !6 = !{i32 7, !"openmp-device", i32 50} -!7 = !{i32 7, !"PIC Level", i32 2} +!7 = !{i32 8, !"PIC Level", i32 2} !8 = !{i32 7, !"frame-pointer", i32 2} !9 = !{!"clang version 14.0.0"} ;.