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 {
822822 /// This is denormalized data.
823823 struct_field_map: std.AutoHashMapUnmanaged(ZigStructField, c_uint),
824824
825 /// Values for `@llvm.compiler.used`.
826 compiler_used: std.ArrayListUnmanaged(Builder.Constant),
825 /// Values for `@llvm.used`.
826 used: std.ArrayListUnmanaged(Builder.Constant),
827827
828828 const ZigStructField = struct {
829829 struct_ty: InternPool.Index,
......@@ -978,7 +978,7 @@ pub const Object = struct {
978978 .error_name_table = .none,
979979 .null_opt_usize = .no_init,
980980 .struct_field_map = .{},
981 .compiler_used = .{},
981 .used = .{},
982982 };
983983 return obj;
984984 }
......@@ -1110,11 +1110,11 @@ pub const Object = struct {
11101110 try o.genCmpLtErrorsLenFunction();
11111111 try o.genModuleLevelAssembly();
11121112
1113 if (o.compiler_used.items.len > 0) {
1114 const array_llvm_ty = try o.builder.arrayType(o.compiler_used.items.len, .ptr);
1115 const init_val = try o.builder.arrayConst(array_llvm_ty, o.compiler_used.items);
1113 if (o.used.items.len > 0) {
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.used.items);
11161116 const compiler_used_variable = try o.builder.addVariable(
1117 try o.builder.strtabString("llvm.compiler.used"),
1117 try o.builder.strtabString("llvm.used"),
11181118 array_llvm_ty,
11191119 .default,
11201120 );
......@@ -1317,7 +1317,8 @@ pub const Object = struct {
13171317 // Zig emits its own PC table instrumentation.
13181318 .PCTable = false,
13191319 .NoPrune = false,
1320 .StackDepth = false,
1320 // Workaround for https://github.com/llvm/llvm-project/pull/106464
1321 .StackDepth = true,
13211322 .TraceLoads = false,
13221323 .TraceStores = false,
13231324 .CollectControlFlow = false,
......@@ -1686,7 +1687,10 @@ pub const Object = struct {
16861687 // The void type used here is a placeholder to be replaced with an
16871688 // 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));
16901694 counters_variable.setLinkage(.private, &o.builder);
16911695 counters_variable.setAlignment(comptime Builder.Alignment.fromByteUnits(1), &o.builder);
16921696 counters_variable.setSection(try o.builder.string("__sancov_cntrs"), &o.builder);
......@@ -1741,17 +1745,15 @@ pub const Object = struct {
17411745
17421746 const array_llvm_ty = try o.builder.arrayType(f.pcs.items.len, .ptr);
17431747 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));
17451752 pcs_variable.setLinkage(.private, &o.builder);
17461753 pcs_variable.setMutability(.constant, &o.builder);
17471754 pcs_variable.setAlignment(Type.usize.abiAlignment(zcu).toLlvm(), &o.builder);
17481755 pcs_variable.setSection(try o.builder.string("__sancov_pcs1"), &o.builder);
17491756 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 });
17551757 }
17561758
17571759 try fg.wip.finish();