authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-19 16:29:45-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-27 15:04:17-07:00
loge9a3ec7e1e0536dcc833acff3cb8745b6e8df848
treef2fb31f5f03c1433929054670c98fbf2ce1015a8
parent1a178d499537b922ff05c5d0186ed5a00dbb1a9b

llvm.Builder: add !nosanitize API

see #20992 Co-authored-by: Jacob Young <jacobly0@users.noreply.github.com>

1 files changed, 39 insertions(+), 3 deletions(-)

src/codegen/llvm/Builder.zig+39-3
...@@ -3994,6 +3994,7 @@ pub const Function = struct {...@@ -3994,6 +3994,7 @@ pub const Function = struct {
3994 names: [*]const String = &[0]String{},3994 names: [*]const String = &[0]String{},
3995 value_indices: [*]const u32 = &[0]u32{},3995 value_indices: [*]const u32 = &[0]u32{},
3996 strip: bool,3996 strip: bool,
3997 any_nosanitize: bool,
3997 debug_locations: std.AutoHashMapUnmanaged(Instruction.Index, DebugLocation) = .{},3998 debug_locations: std.AutoHashMapUnmanaged(Instruction.Index, DebugLocation) = .{},
3998 debug_values: []const Instruction.Index = &.{},3999 debug_values: []const Instruction.Index = &.{},
3999 extra: []const u32 = &.{},4000 extra: []const u32 = &.{},
...@@ -4090,6 +4091,7 @@ pub const Function = struct {...@@ -4090,6 +4091,7 @@ pub const Function = struct {
4090 block,4091 block,
4091 br,4092 br,
4092 br_cond,4093 br_cond,
4094 br_cond_nosanitize,
4093 call,4095 call,
4094 @"call fast",4096 @"call fast",
4095 cmpxchg,4097 cmpxchg,
...@@ -4345,6 +4347,13 @@ pub const Function = struct {...@@ -4345,6 +4347,13 @@ pub const Function = struct {
4345 else => unreachable,4347 else => unreachable,
4346 };4348 };
4347 }4349 }
4350
4351 pub fn isNosanitize(self: Tag) bool {
4352 return switch (self) {
4353 .br_cond_nosanitize => true,
4354 else => false,
4355 };
4356 }
4348 };4357 };
43494358
4350 pub const Index = enum(u32) {4359 pub const Index = enum(u32) {
...@@ -4367,6 +4376,7 @@ pub const Function = struct {...@@ -4367,6 +4376,7 @@ pub const Function = struct {
4367 return switch (wip.instructions.items(.tag)[@intFromEnum(self)]) {4376 return switch (wip.instructions.items(.tag)[@intFromEnum(self)]) {
4368 .br,4377 .br,
4369 .br_cond,4378 .br_cond,
4379 .br_cond_nosanitize,
4370 .ret,4380 .ret,
4371 .@"ret void",4381 .@"ret void",
4372 .@"switch",4382 .@"switch",
...@@ -4380,6 +4390,7 @@ pub const Function = struct {...@@ -4380,6 +4390,7 @@ pub const Function = struct {
4380 return switch (wip.instructions.items(.tag)[@intFromEnum(self)]) {4390 return switch (wip.instructions.items(.tag)[@intFromEnum(self)]) {
4381 .br,4391 .br,
4382 .br_cond,4392 .br_cond,
4393 .br_cond_nosanitize,
4383 .fence,4394 .fence,
4384 .ret,4395 .ret,
4385 .@"ret void",4396 .@"ret void",
...@@ -4470,6 +4481,7 @@ pub const Function = struct {...@@ -4470,6 +4481,7 @@ pub const Function = struct {
4470 .block => .label,4481 .block => .label,
4471 .br,4482 .br,
4472 .br_cond,4483 .br_cond,
4484 .br_cond_nosanitize,
4473 .fence,4485 .fence,
4474 .ret,4486 .ret,
4475 .@"ret void",4487 .@"ret void",
...@@ -4656,6 +4668,7 @@ pub const Function = struct {...@@ -4656,6 +4668,7 @@ pub const Function = struct {
4656 .block => .label,4668 .block => .label,
4657 .br,4669 .br,
4658 .br_cond,4670 .br_cond,
4671 .br_cond_nosanitize,
4659 .fence,4672 .fence,
4660 .ret,4673 .ret,
4661 .@"ret void",4674 .@"ret void",
...@@ -5100,6 +5113,7 @@ pub const WipFunction = struct {...@@ -5100,6 +5113,7 @@ pub const WipFunction = struct {
5100 instructions: std.MultiArrayList(Instruction),5113 instructions: std.MultiArrayList(Instruction),
5101 names: std.ArrayListUnmanaged(String),5114 names: std.ArrayListUnmanaged(String),
5102 strip: bool,5115 strip: bool,
5116 any_nosanitize: bool,
5103 debug_locations: std.AutoArrayHashMapUnmanaged(Instruction.Index, DebugLocation),5117 debug_locations: std.AutoArrayHashMapUnmanaged(Instruction.Index, DebugLocation),
5104 debug_values: std.AutoArrayHashMapUnmanaged(Instruction.Index, void),5118 debug_values: std.AutoArrayHashMapUnmanaged(Instruction.Index, void),
5105 extra: std.ArrayListUnmanaged(u32),5119 extra: std.ArrayListUnmanaged(u32),
...@@ -5146,6 +5160,7 @@ pub const WipFunction = struct {...@@ -5146,6 +5160,7 @@ pub const WipFunction = struct {
5146 .instructions = .{},5160 .instructions = .{},
5147 .names = .{},5161 .names = .{},
5148 .strip = options.strip,5162 .strip = options.strip,
5163 .any_nosanitize = false,
5149 .debug_locations = .{},5164 .debug_locations = .{},
5150 .debug_values = .{},5165 .debug_values = .{},
5151 .extra = .{},5166 .extra = .{},
...@@ -6437,7 +6452,7 @@ pub const WipFunction = struct {...@@ -6437,7 +6452,7 @@ pub const WipFunction = struct {
6437 .@"ret void",6452 .@"ret void",
6438 .@"unreachable",6453 .@"unreachable",
6439 => {},6454 => {},
6440 .br_cond => {6455 .br_cond, .br_cond_nosanitize => {
6441 const extra = self.extraData(Instruction.BrCond, instruction.data);6456 const extra = self.extraData(Instruction.BrCond, instruction.data);
6442 instruction.data = wip_extra.addExtra(Instruction.BrCond{6457 instruction.data = wip_extra.addExtra(Instruction.BrCond{
6443 .cond = instructions.map(extra.cond),6458 .cond = instructions.map(extra.cond),
...@@ -6624,6 +6639,7 @@ pub const WipFunction = struct {...@@ -6624,6 +6639,7 @@ pub const WipFunction = struct {
6624 function.names = names.ptr;6639 function.names = names.ptr;
6625 function.value_indices = value_indices.ptr;6640 function.value_indices = value_indices.ptr;
6626 function.strip = self.strip;6641 function.strip = self.strip;
6642 function.any_nosanitize = self.any_nosanitize;
6627 function.debug_locations = debug_locations;6643 function.debug_locations = debug_locations;
6628 function.debug_values = debug_values;6644 function.debug_values = debug_values;
6629 }6645 }
...@@ -8537,6 +8553,7 @@ pub fn init(options: Options) Allocator.Error!Builder {...@@ -8537,6 +8553,7 @@ pub fn init(options: Options) Allocator.Error!Builder {
85378553
8538 try self.metadata_string_indices.append(self.gpa, 0);8554 try self.metadata_string_indices.append(self.gpa, 0);
8539 assert(try self.metadataString("") == .none);8555 assert(try self.metadataString("") == .none);
8556 assert(try self.debugTuple(&.{}) == .empty_tuple);
85408557
8541 return self;8558 return self;
8542}8559}
...@@ -8934,6 +8951,7 @@ pub fn addFunctionAssumeCapacity(...@@ -8934,6 +8951,7 @@ pub fn addFunctionAssumeCapacity(
8934 .kind = .{ .function = function_index },8951 .kind = .{ .function = function_index },
8935 }),8952 }),
8936 .strip = undefined,8953 .strip = undefined,
8954 .any_nosanitize = false,
8937 });8955 });
8938 return function_index;8956 return function_index;
8939}8957}
...@@ -9581,6 +9599,12 @@ pub fn printUnbuffered(...@@ -9581,6 +9599,12 @@ pub fn printUnbuffered(
9581 });9599 });
9582 }9600 }
9583 if (function.instructions.len > 0) {9601 if (function.instructions.len > 0) {
9602 const maybe_empty_tuple: ?u32 = if (!function.any_nosanitize) null else b: {
9603 const gop = try metadata_formatter.map.getOrPut(self.gpa, .{
9604 .metadata = .empty_tuple,
9605 });
9606 break :b @intCast(gop.index);
9607 };
9584 var block_incoming_len: u32 = undefined;9608 var block_incoming_len: u32 = undefined;
9585 try writer.writeAll(" {\n");9609 try writer.writeAll(" {\n");
9586 var maybe_dbg_index: ?u32 = null;9610 var maybe_dbg_index: ?u32 = null;
...@@ -9770,6 +9794,14 @@ pub fn printUnbuffered(...@@ -9770,6 +9794,14 @@ pub fn printUnbuffered(
9770 }),9794 }),
9771 }9795 }
9772 },9796 },
9797 .br_cond_nosanitize => {
9798 const extra = function.extraData(Function.Instruction.BrCond, instruction.data);
9799 try writer.print(" br {%}, {%}, {%}", .{
9800 extra.cond.fmt(function_index, self),
9801 extra.then.toInst(&function).fmt(function_index, self),
9802 extra.@"else".toInst(&function).fmt(function_index, self),
9803 });
9804 },
9773 .call,9805 .call,
9774 .@"call fast",9806 .@"call fast",
9775 .@"musttail call",9807 .@"musttail call",
...@@ -10046,8 +10078,12 @@ pub fn printUnbuffered(...@@ -10046,8 +10078,12 @@ pub fn printUnbuffered(
10046 }10078 }
1004710079
10048 if (maybe_dbg_index) |dbg_index| {10080 if (maybe_dbg_index) |dbg_index| {
10049 try writer.print(", !dbg !{}\n", .{dbg_index});10081 try writer.print(", !dbg !{}", .{dbg_index});
10050 } else try writer.writeByte('\n');10082 }
10083 if (instruction.tag.isNosanitize()) {
10084 try writer.print(", !nosanitize !{d}", .{maybe_empty_tuple.?});
10085 }
10086 try writer.writeByte('\n');
10051 }10087 }
10052 try writer.writeByte('}');10088 try writer.writeByte('}');
10053 }10089 }