authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-28 16:57:43-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-28 18:07:13-07:00
logc81219c573f75a2163dfaae5bae7d7373e179f91
tree88e004412876297b8b4842c618f5d7270091e59e
parent9e11c4f60ea56af50bed7bb27984307762d5f167

LLVM: use `@llvm.used` instead of `@llvm.compiler.used`

because it marks the linker section, preventing garbage collection. Also, name the members because that is required by this intrinsic. Also, enable the StackDepth option in the sancov pass as a workaround for https://github.com/llvm/llvm-project/pull/106464, otherwise, LLVM enables TracePCGuard even though we explicitly disable it.

1 files changed, 17 insertions(+), 15 deletions(-)

src/codegen/llvm.zig+17-15
...@@ -822,8 +822,8 @@ pub const Object = struct {...@@ -822,8 +822,8 @@ pub const Object = struct {
822 /// This is denormalized data.822 /// This is denormalized data.
823 struct_field_map: std.AutoHashMapUnmanaged(ZigStructField, c_uint),823 struct_field_map: std.AutoHashMapUnmanaged(ZigStructField, c_uint),
824824
825 /// Values for `@llvm.compiler.used`.825 /// Values for `@llvm.used`.
826 compiler_used: std.ArrayListUnmanaged(Builder.Constant),826 used: std.ArrayListUnmanaged(Builder.Constant),
827827
828 const ZigStructField = struct {828 const ZigStructField = struct {
829 struct_ty: InternPool.Index,829 struct_ty: InternPool.Index,
...@@ -978,7 +978,7 @@ pub const Object = struct {...@@ -978,7 +978,7 @@ pub const Object = struct {
978 .error_name_table = .none,978 .error_name_table = .none,
979 .null_opt_usize = .no_init,979 .null_opt_usize = .no_init,
980 .struct_field_map = .{},980 .struct_field_map = .{},
981 .compiler_used = .{},981 .used = .{},
982 };982 };
983 return obj;983 return obj;
984 }984 }
...@@ -1110,11 +1110,11 @@ pub const Object = struct {...@@ -1110,11 +1110,11 @@ pub const Object = struct {
1110 try o.genCmpLtErrorsLenFunction();1110 try o.genCmpLtErrorsLenFunction();
1111 try o.genModuleLevelAssembly();1111 try o.genModuleLevelAssembly();
11121112
1113 if (o.compiler_used.items.len > 0) {1113 if (o.used.items.len > 0) {
1114 const array_llvm_ty = try o.builder.arrayType(o.compiler_used.items.len, .ptr);1114 const array_llvm_ty = try o.builder.arrayType(o.used.items.len, .ptr);
1115 const init_val = try o.builder.arrayConst(array_llvm_ty, o.compiler_used.items);1115 const init_val = try o.builder.arrayConst(array_llvm_ty, o.used.items);
1116 const compiler_used_variable = try o.builder.addVariable(1116 const compiler_used_variable = try o.builder.addVariable(
1117 try o.builder.strtabString("llvm.compiler.used"),1117 try o.builder.strtabString("llvm.used"),
1118 array_llvm_ty,1118 array_llvm_ty,
1119 .default,1119 .default,
1120 );1120 );
...@@ -1317,7 +1317,8 @@ pub const Object = struct {...@@ -1317,7 +1317,8 @@ pub const Object = struct {
1317 // Zig emits its own PC table instrumentation.1317 // Zig emits its own PC table instrumentation.
1318 .PCTable = false,1318 .PCTable = false,
1319 .NoPrune = false,1319 .NoPrune = false,
1320 .StackDepth = false,1320 // Workaround for https://github.com/llvm/llvm-project/pull/106464
1321 .StackDepth = true,
1321 .TraceLoads = false,1322 .TraceLoads = false,
1322 .TraceStores = false,1323 .TraceStores = false,
1323 .CollectControlFlow = false,1324 .CollectControlFlow = false,
...@@ -1686,7 +1687,10 @@ pub const Object = struct {...@@ -1686,7 +1687,10 @@ pub const Object = struct {
1686 // The void type used here is a placeholder to be replaced with an1687 // The void type used here is a placeholder to be replaced with an
1687 // array of the appropriate size after the POI count is known.1688 // array of the appropriate size after the POI count is known.
16881689
1689 const counters_variable = try o.builder.addVariable(.empty, .void, .default);1690 // Due to error "members of llvm.compiler.used must be named", this global needs a name.
1691 const anon_name = try o.builder.strtabStringFmt("__sancov_gen_.{d}", .{o.used.items.len});
1692 const counters_variable = try o.builder.addVariable(anon_name, .void, .default);
1693 try o.used.append(gpa, counters_variable.toConst(&o.builder));
1690 counters_variable.setLinkage(.private, &o.builder);1694 counters_variable.setLinkage(.private, &o.builder);
1691 counters_variable.setAlignment(comptime Builder.Alignment.fromByteUnits(1), &o.builder);1695 counters_variable.setAlignment(comptime Builder.Alignment.fromByteUnits(1), &o.builder);
1692 counters_variable.setSection(try o.builder.string("__sancov_cntrs"), &o.builder);1696 counters_variable.setSection(try o.builder.string("__sancov_cntrs"), &o.builder);
...@@ -1741,17 +1745,15 @@ pub const Object = struct {...@@ -1741,17 +1745,15 @@ pub const Object = struct {
17411745
1742 const array_llvm_ty = try o.builder.arrayType(f.pcs.items.len, .ptr);1746 const array_llvm_ty = try o.builder.arrayType(f.pcs.items.len, .ptr);
1743 const init_val = try o.builder.arrayConst(array_llvm_ty, f.pcs.items);1747 const init_val = try o.builder.arrayConst(array_llvm_ty, f.pcs.items);
1744 const pcs_variable = try o.builder.addVariable(.empty, array_llvm_ty, .default);1748 // Due to error "members of llvm.compiler.used must be named", this global needs a name.
1749 const anon_name = try o.builder.strtabStringFmt("__sancov_gen_.{d}", .{o.used.items.len});
1750 const pcs_variable = try o.builder.addVariable(anon_name, array_llvm_ty, .default);
1751 try o.used.append(gpa, pcs_variable.toConst(&o.builder));
1745 pcs_variable.setLinkage(.private, &o.builder);1752 pcs_variable.setLinkage(.private, &o.builder);
1746 pcs_variable.setMutability(.constant, &o.builder);1753 pcs_variable.setMutability(.constant, &o.builder);
1747 pcs_variable.setAlignment(Type.usize.abiAlignment(zcu).toLlvm(), &o.builder);1754 pcs_variable.setAlignment(Type.usize.abiAlignment(zcu).toLlvm(), &o.builder);
1748 pcs_variable.setSection(try o.builder.string("__sancov_pcs1"), &o.builder);1755 pcs_variable.setSection(try o.builder.string("__sancov_pcs1"), &o.builder);
1749 try pcs_variable.setInitializer(init_val, &o.builder);1756 try pcs_variable.setInitializer(init_val, &o.builder);
1750
1751 try o.compiler_used.appendSlice(gpa, &.{
1752 f.counters_variable.toConst(&o.builder),
1753 pcs_variable.toConst(&o.builder),
1754 });
1755 }1757 }
17561758
1757 try fg.wip.finish();1759 try fg.wip.finish();