authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-30 14:47:43-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-08-30 14:47:43-07:00
loge4e91a1314abd78a0f06df718459e42e3c9fb62a
tree0ea10dc19eb14e62fefc329e1ead6110e7c0d5bd
parent5d08b7f0548c61b918504a7b02573127c49c6f97
parenta2e691d589ac1d234ee624fdf8fa931d89a719b1
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #21224 from alexrp/mips-gnu-fixes

Fix MIPS PIC level and work around an LLVM bug for `mips(el)-linux-gnueabi(hf)`

6 files changed, 34 insertions(+), 27 deletions(-)

lib/std/zig/Server.zig+3-14
......@@ -124,9 +124,9 @@ pub fn receiveMessage(s: *Server) !InMessage.Header {
124124 const buf = fifo.readableSlice(0);
125125 assert(fifo.readableLength() == buf.len);
126126 if (buf.len >= @sizeOf(Header)) {
127 // workaround for https://github.com/ziglang/zig/issues/14904
128 const bytes_len = bswap_and_workaround_u32(buf[4..][0..4]);
129 const tag = bswap_and_workaround_tag(buf[0..][0..4]);
127 const header: *align(1) const Header = @ptrCast(buf[0..@sizeOf(Header)]);
128 const bytes_len = bswap(header.bytes_len);
129 const tag = bswap(header.tag);
130130
131131 if (buf.len - @sizeOf(Header) >= bytes_len) {
132132 fifo.discard(@sizeOf(Header));
......@@ -307,17 +307,6 @@ fn bswap_u32_array(slice: []u32) void {
307307 for (slice) |*elem| elem.* = @byteSwap(elem.*);
308308}
309309
310/// workaround for https://github.com/ziglang/zig/issues/14904
311fn bswap_and_workaround_u32(bytes_ptr: *const [4]u8) u32 {
312 return std.mem.readInt(u32, bytes_ptr, .little);
313}
314
315/// workaround for https://github.com/ziglang/zig/issues/14904
316fn bswap_and_workaround_tag(bytes_ptr: *const [4]u8) InMessage.Tag {
317 const int = std.mem.readInt(u32, bytes_ptr, .little);
318 return @as(InMessage.Tag, @enumFromInt(int));
319}
320
321310const OutMessage = std.zig.Server.Message;
322311const InMessage = std.zig.Client.Message;
323312
src/codegen/llvm.zig+7-2
......@@ -1276,8 +1276,11 @@ pub const Object = struct {
12761276 );
12771277 errdefer target_machine.dispose();
12781278
1279 if (pic) module.setModulePICLevel();
1280 if (comp.config.pie) module.setModulePIELevel();
1279 const large_pic = target_util.usesLargePIC(comp.root_mod.resolved_target.result);
1280
1281 if (pic) module.setModulePICLevel(large_pic);
1282 if (comp.config.pie) module.setModulePIELevel(large_pic);
1283
12811284 if (code_model != .Default) module.setModuleCodeModel(code_model);
12821285
12831286 if (comp.llvm_opt_bisect_limit >= 0) {
......@@ -1294,6 +1297,8 @@ pub const Object = struct {
12941297 .tsan = options.sanitize_thread,
12951298 .sancov = options.fuzz,
12961299 .lto = options.lto,
1300 // https://github.com/ziglang/zig/issues/21215
1301 .allow_fast_isel = !comp.root_mod.resolved_target.result.cpu.arch.isMIPS(),
12971302 .asm_filename = null,
12981303 .bin_filename = options.bin_path,
12991304 .llvm_ir_filename = options.post_ir_path,
src/codegen/llvm/bindings.zig+3-2
......@@ -53,10 +53,10 @@ pub const Module = opaque {
5353 extern fn LLVMDisposeModule(*Module) void;
5454
5555 pub const setModulePICLevel = ZigLLVMSetModulePICLevel;
56 extern fn ZigLLVMSetModulePICLevel(module: *Module) void;
56 extern fn ZigLLVMSetModulePICLevel(module: *Module, big: bool) void;
5757
5858 pub const setModulePIELevel = ZigLLVMSetModulePIELevel;
59 extern fn ZigLLVMSetModulePIELevel(module: *Module) void;
59 extern fn ZigLLVMSetModulePIELevel(module: *Module, large: bool) void;
6060
6161 pub const setModuleCodeModel = ZigLLVMSetModuleCodeModel;
6262 extern fn ZigLLVMSetModuleCodeModel(module: *Module, code_model: CodeModel) void;
......@@ -91,6 +91,7 @@ pub const TargetMachine = opaque {
9191 tsan: bool,
9292 sancov: bool,
9393 lto: bool,
94 allow_fast_isel: bool,
9495 asm_filename: ?[*:0]const u8,
9596 bin_filename: ?[*:0]const u8,
9697 llvm_ir_filename: ?[*:0]const u8,
src/target.zig+6
......@@ -49,6 +49,12 @@ pub fn requiresPIC(target: std.Target, linking_libc: bool) bool {
4949 (target.abi == .ohos and target.cpu.arch == .aarch64);
5050}
5151
52pub fn usesLargePIC(target: std.Target) bool {
53 // MIPS always uses PIC level 1; other platforms vary in their default PIC levels, but they
54 // support both level 1 and 2, in which case we prefer 2.
55 return !target.cpu.arch.isMIPS();
56}
57
5258/// This is not whether the target supports Position Independent Code, but whether the -fPIC
5359/// C compiler argument is valid to Clang.
5460pub fn supports_fpic(target: std.Target) bool {
src/zig_llvm.cpp+11-6
......@@ -81,7 +81,7 @@ static const bool assertions_on = false;
8181
8282LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
8383 const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
84 LLVMCodeModel CodeModel, bool function_sections, bool data_sections, ZigLLVMABIType float_abi,
84 LLVMCodeModel CodeModel, bool function_sections, bool data_sections, ZigLLVMABIType float_abi,
8585 const char *abi_name)
8686{
8787 std::optional<Reloc::Model> RM;
......@@ -258,7 +258,6 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
258258 options->bin_filename? options->bin_filename : options->asm_filename);
259259
260260 TargetMachine &target_machine = *reinterpret_cast<TargetMachine*>(targ_machine_ref);
261 target_machine.setO0WantsFastISel(true);
262261
263262 Module &llvm_module = *unwrap(module_ref);
264263
......@@ -369,6 +368,12 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
369368 }
370369 }
371370
371 if (options.allow_fast_isel) {
372 target_machine.setO0WantsFastISel(true);
373 } else {
374 target_machine.setFastISel(false);
375 }
376
372377 // Optimization phase
373378 module_pm.run(llvm_module, module_am);
374379
......@@ -430,12 +435,12 @@ void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv) {
430435 cl::ParseCommandLineOptions(argc, argv);
431436}
432437
433void ZigLLVMSetModulePICLevel(LLVMModuleRef module) {
434 unwrap(module)->setPICLevel(PICLevel::Level::BigPIC);
438void ZigLLVMSetModulePICLevel(LLVMModuleRef module, bool big) {
439 unwrap(module)->setPICLevel(big ? PICLevel::Level::BigPIC : PICLevel::Level::SmallPIC);
435440}
436441
437void ZigLLVMSetModulePIELevel(LLVMModuleRef module) {
438 unwrap(module)->setPIELevel(PIELevel::Level::Large);
442void ZigLLVMSetModulePIELevel(LLVMModuleRef module, bool large) {
443 unwrap(module)->setPIELevel(large ? PIELevel::Level::Large : PIELevel::Level::Small);
439444}
440445
441446void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model) {
src/zig_llvm.h+4-3
......@@ -59,6 +59,7 @@ struct ZigLLVMEmitOptions {
5959 bool tsan;
6060 bool sancov;
6161 bool lto;
62 bool allow_fast_isel;
6263 const char *asm_filename;
6364 const char *bin_filename;
6465 const char *llvm_ir_filename;
......@@ -77,7 +78,7 @@ enum ZigLLVMABIType {
7778
7879ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
7980 const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
80 LLVMCodeModel CodeModel, bool function_sections, bool data_sections, enum ZigLLVMABIType float_abi,
81 LLVMCodeModel CodeModel, bool function_sections, bool data_sections, enum ZigLLVMABIType float_abi,
8182 const char *abi_name);
8283
8384ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);
......@@ -154,8 +155,8 @@ enum ZigLLVM_CallingConv {
154155 ZigLLVM_MaxID = 1023,
155156};
156157
157ZIG_EXTERN_C void ZigLLVMSetModulePICLevel(LLVMModuleRef module);
158ZIG_EXTERN_C void ZigLLVMSetModulePIELevel(LLVMModuleRef module);
158ZIG_EXTERN_C void ZigLLVMSetModulePICLevel(LLVMModuleRef module, bool big);
159ZIG_EXTERN_C void ZigLLVMSetModulePIELevel(LLVMModuleRef module, bool large);
159160ZIG_EXTERN_C void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model);
160161
161162ZIG_EXTERN_C void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv);