Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

llvm: begin the journey of independence from llvm #16430

Merged
merged 17 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions lib/std/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1357,8 +1357,6 @@ pub const Target = struct {
}
};

pub const stack_align = 16;

pub fn zigTriple(self: Target, allocator: mem.Allocator) ![]u8 {
return std.zig.CrossTarget.fromTarget(self).zigTriple(allocator);
}
Expand Down Expand Up @@ -1833,7 +1831,7 @@ pub const Target = struct {
};
}

pub fn ptrBitWidth(target: std.Target) u16 {
pub fn ptrBitWidth(target: Target) u16 {
switch (target.abi) {
.gnux32, .muslx32, .gnuabin32, .gnuilp32 => return 32,
.gnuabi64 => return 64,
Expand Down Expand Up @@ -1910,6 +1908,43 @@ pub const Target = struct {
}
}

pub fn stackAlignment(target: Target) u16 {
return switch (target.cpu.arch) {
.amdgcn => 4,
.x86 => switch (target.os.tag) {
.windows => 4,
else => 16,
},
.arm,
.armeb,
.thumb,
.thumbeb,
.mips,
.mipsel,
.sparc,
.sparcel,
=> 8,
.aarch64,
.aarch64_be,
.aarch64_32,
.bpfeb,
.bpfel,
.mips64,
.mips64el,
.powerpc64,
.powerpc64le,
.riscv32,
.riscv64,
.sparc64,
.x86_64,
.ve,
.wasm32,
.wasm64,
=> 16,
else => @divExact(target.ptrBitWidth(), 8),
};
}

/// Default signedness of `char` for the native C compiler for this target
/// Note that char signedness is implementation-defined and many compilers provide
/// an option to override the default signedness e.g. GCC's -funsigned-char / -fsigned-char
Expand Down Expand Up @@ -2428,7 +2463,7 @@ pub const Target = struct {
else => {},
},
.avr => switch (c_type) {
.int, .uint, .long, .ulong, .float, .longdouble => return 1,
.char, .int, .uint, .long, .ulong, .float, .longdouble => return 1,
.short, .ushort => return 2,
.double => return 4,
.longlong, .ulonglong => return 8,
Expand Down
2 changes: 1 addition & 1 deletion lib/test_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn mainTerminal() void {
const have_tty = progress.terminal != null and
(progress.supports_ansi_escape_codes or progress.is_windows_terminal);

var async_frame_buffer: []align(std.Target.stack_align) u8 = undefined;
var async_frame_buffer: []align(builtin.target.stackAlignment()) u8 = undefined;
// TODO this is on the next line (using `undefined` above) because otherwise zig incorrectly
// ignores the alignment of the slice.
async_frame_buffer = &[_]u8{};
Expand Down
6 changes: 5 additions & 1 deletion src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ pub const InitOptions = struct {
want_lto: ?bool = null,
want_unwind_tables: ?bool = null,
use_llvm: ?bool = null,
use_lib_llvm: ?bool = null,
use_lld: ?bool = null,
use_clang: ?bool = null,
single_threaded: ?bool = null,
Expand Down Expand Up @@ -753,7 +754,8 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
const root_name = try arena.dupeZ(u8, options.root_name);

// Make a decision on whether to use LLVM or our own backend.
const use_llvm = build_options.have_llvm and blk: {
const use_lib_llvm = options.use_lib_llvm orelse build_options.have_llvm;
const use_llvm = blk: {
if (options.use_llvm) |explicit|
break :blk explicit;

Expand Down Expand Up @@ -1161,6 +1163,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
hash.add(valgrind);
hash.add(single_threaded);
hash.add(use_llvm);
hash.add(use_lib_llvm);
hash.add(dll_export_fns);
hash.add(options.is_test);
hash.add(options.test_evented_io);
Expand Down Expand Up @@ -1444,6 +1447,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
.optimize_mode = options.optimize_mode,
.use_lld = use_lld,
.use_llvm = use_llvm,
.use_lib_llvm = use_lib_llvm,
.link_libc = link_libc,
.link_libcpp = link_libcpp,
.link_libunwind = link_libunwind,
Expand Down
12 changes: 4 additions & 8 deletions src/Module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -835,10 +835,6 @@ pub const Decl = struct {
assert(decl.has_tv);
return @as(u32, @intCast(decl.alignment.toByteUnitsOptional() orelse decl.ty.abiAlignment(mod)));
}

pub fn intern(decl: *Decl, mod: *Module) Allocator.Error!void {
decl.val = (try decl.val.intern(decl.ty, mod)).toValue();
}
};

/// This state is attached to every Decl when Module emit_h is non-null.
Expand Down Expand Up @@ -4204,7 +4200,7 @@ pub fn semaFile(mod: *Module, file: *File) SemaError!void {
try wip_captures.finalize();
for (comptime_mutable_decls.items) |decl_index| {
const decl = mod.declPtr(decl_index);
try decl.intern(mod);
_ = try decl.internValue(mod);
}
new_decl.analysis = .complete;
} else |err| switch (err) {
Expand Down Expand Up @@ -4315,7 +4311,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
try wip_captures.finalize();
for (comptime_mutable_decls.items) |ct_decl_index| {
const ct_decl = mod.declPtr(ct_decl_index);
try ct_decl.intern(mod);
_ = try ct_decl.internValue(mod);
}
const align_src: LazySrcLoc = .{ .node_offset_var_decl_align = 0 };
const section_src: LazySrcLoc = .{ .node_offset_var_decl_section = 0 };
Expand Down Expand Up @@ -5362,7 +5358,7 @@ pub fn analyzeFnBody(mod: *Module, func_index: InternPool.Index, arena: Allocato
try wip_captures.finalize();
for (comptime_mutable_decls.items) |ct_decl_index| {
const ct_decl = mod.declPtr(ct_decl_index);
try ct_decl.intern(mod);
_ = try ct_decl.internValue(mod);
}

// Copy the block into place and mark that as the main block.
Expand Down Expand Up @@ -6369,7 +6365,7 @@ pub fn markDeclAlive(mod: *Module, decl: *Decl) Allocator.Error!void {
if (decl.alive) return;
decl.alive = true;

try decl.intern(mod);
_ = try decl.internValue(mod);

// This is the first time we are marking this Decl alive. We must
// therefore recurse into its value and mark any Decl it references
Expand Down
8 changes: 4 additions & 4 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3899,7 +3899,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
try mod.declareDeclDependency(sema.owner_decl_index, decl_index);

const decl = mod.declPtr(decl_index);
if (iac.is_const) try decl.intern(mod);
if (iac.is_const) _ = try decl.internValue(mod);
const final_elem_ty = decl.ty;
const final_ptr_ty = try mod.ptrType(.{
.child = final_elem_ty.toIntern(),
Expand Down Expand Up @@ -33577,7 +33577,7 @@ fn semaBackingIntType(mod: *Module, struct_obj: *Module.Struct) CompileError!voi
try wip_captures.finalize();
for (comptime_mutable_decls.items) |ct_decl_index| {
const ct_decl = mod.declPtr(ct_decl_index);
try ct_decl.intern(mod);
_ = try ct_decl.internValue(mod);
}
} else {
if (fields_bit_sum > std.math.maxInt(u16)) {
Expand Down Expand Up @@ -34645,7 +34645,7 @@ fn semaStructFields(mod: *Module, struct_obj: *Module.Struct) CompileError!void
try wip_captures.finalize();
for (comptime_mutable_decls.items) |ct_decl_index| {
const ct_decl = mod.declPtr(ct_decl_index);
try ct_decl.intern(mod);
_ = try ct_decl.internValue(mod);
}

struct_obj.have_field_inits = true;
Expand Down Expand Up @@ -34744,7 +34744,7 @@ fn semaUnionFields(mod: *Module, union_obj: *Module.Union) CompileError!void {
try wip_captures.finalize();
for (comptime_mutable_decls.items) |ct_decl_index| {
const ct_decl = mod.declPtr(ct_decl_index);
try ct_decl.intern(mod);
_ = try ct_decl.internValue(mod);
}

try union_obj.fields.ensureTotalCapacity(mod.tmp_hack_arena.allocator(), fields_len);
Expand Down
Loading