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 {...@@ -124,9 +124,9 @@ pub fn receiveMessage(s: *Server) !InMessage.Header {
124 const buf = fifo.readableSlice(0);124 const buf = fifo.readableSlice(0);
125 assert(fifo.readableLength() == buf.len);125 assert(fifo.readableLength() == buf.len);
126 if (buf.len >= @sizeOf(Header)) {126 if (buf.len >= @sizeOf(Header)) {
127 // workaround for https://github.com/ziglang/zig/issues/14904127 const header: *align(1) const Header = @ptrCast(buf[0..@sizeOf(Header)]);
128 const bytes_len = bswap_and_workaround_u32(buf[4..][0..4]);128 const bytes_len = bswap(header.bytes_len);
129 const tag = bswap_and_workaround_tag(buf[0..][0..4]);129 const tag = bswap(header.tag);
130130
131 if (buf.len - @sizeOf(Header) >= bytes_len) {131 if (buf.len - @sizeOf(Header) >= bytes_len) {
132 fifo.discard(@sizeOf(Header));132 fifo.discard(@sizeOf(Header));
...@@ -307,17 +307,6 @@ fn bswap_u32_array(slice: []u32) void {...@@ -307,17 +307,6 @@ fn bswap_u32_array(slice: []u32) void {
307 for (slice) |*elem| elem.* = @byteSwap(elem.*);307 for (slice) |*elem| elem.* = @byteSwap(elem.*);
308}308}
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
321const OutMessage = std.zig.Server.Message;310const OutMessage = std.zig.Server.Message;
322const InMessage = std.zig.Client.Message;311const InMessage = std.zig.Client.Message;
323312
src/codegen/llvm.zig+7-2
...@@ -1276,8 +1276,11 @@ pub const Object = struct {...@@ -1276,8 +1276,11 @@ pub const Object = struct {
1276 );1276 );
1277 errdefer target_machine.dispose();1277 errdefer target_machine.dispose();
12781278
1279 if (pic) module.setModulePICLevel();1279 const large_pic = target_util.usesLargePIC(comp.root_mod.resolved_target.result);
1280 if (comp.config.pie) module.setModulePIELevel();1280
1281 if (pic) module.setModulePICLevel(large_pic);
1282 if (comp.config.pie) module.setModulePIELevel(large_pic);
1283
1281 if (code_model != .Default) module.setModuleCodeModel(code_model);1284 if (code_model != .Default) module.setModuleCodeModel(code_model);
12821285
1283 if (comp.llvm_opt_bisect_limit >= 0) {1286 if (comp.llvm_opt_bisect_limit >= 0) {
...@@ -1294,6 +1297,8 @@ pub const Object = struct {...@@ -1294,6 +1297,8 @@ pub const Object = struct {
1294 .tsan = options.sanitize_thread,1297 .tsan = options.sanitize_thread,
1295 .sancov = options.fuzz,1298 .sancov = options.fuzz,
1296 .lto = options.lto,1299 .lto = options.lto,
1300 // https://github.com/ziglang/zig/issues/21215
1301 .allow_fast_isel = !comp.root_mod.resolved_target.result.cpu.arch.isMIPS(),
1297 .asm_filename = null,1302 .asm_filename = null,
1298 .bin_filename = options.bin_path,1303 .bin_filename = options.bin_path,
1299 .llvm_ir_filename = options.post_ir_path,1304 .llvm_ir_filename = options.post_ir_path,
src/codegen/llvm/bindings.zig+3-2
...@@ -53,10 +53,10 @@ pub const Module = opaque {...@@ -53,10 +53,10 @@ pub const Module = opaque {
53 extern fn LLVMDisposeModule(*Module) void;53 extern fn LLVMDisposeModule(*Module) void;
5454
55 pub const setModulePICLevel = ZigLLVMSetModulePICLevel;55 pub const setModulePICLevel = ZigLLVMSetModulePICLevel;
56 extern fn ZigLLVMSetModulePICLevel(module: *Module) void;56 extern fn ZigLLVMSetModulePICLevel(module: *Module, big: bool) void;
5757
58 pub const setModulePIELevel = ZigLLVMSetModulePIELevel;58 pub const setModulePIELevel = ZigLLVMSetModulePIELevel;
59 extern fn ZigLLVMSetModulePIELevel(module: *Module) void;59 extern fn ZigLLVMSetModulePIELevel(module: *Module, large: bool) void;
6060
61 pub const setModuleCodeModel = ZigLLVMSetModuleCodeModel;61 pub const setModuleCodeModel = ZigLLVMSetModuleCodeModel;
62 extern fn ZigLLVMSetModuleCodeModel(module: *Module, code_model: CodeModel) void;62 extern fn ZigLLVMSetModuleCodeModel(module: *Module, code_model: CodeModel) void;
...@@ -91,6 +91,7 @@ pub const TargetMachine = opaque {...@@ -91,6 +91,7 @@ pub const TargetMachine = opaque {
91 tsan: bool,91 tsan: bool,
92 sancov: bool,92 sancov: bool,
93 lto: bool,93 lto: bool,
94 allow_fast_isel: bool,
94 asm_filename: ?[*:0]const u8,95 asm_filename: ?[*:0]const u8,
95 bin_filename: ?[*:0]const u8,96 bin_filename: ?[*:0]const u8,
96 llvm_ir_filename: ?[*:0]const u8,97 llvm_ir_filename: ?[*:0]const u8,
src/target.zig+6
...@@ -49,6 +49,12 @@ pub fn requiresPIC(target: std.Target, linking_libc: bool) bool {...@@ -49,6 +49,12 @@ pub fn requiresPIC(target: std.Target, linking_libc: bool) bool {
49 (target.abi == .ohos and target.cpu.arch == .aarch64);49 (target.abi == .ohos and target.cpu.arch == .aarch64);
50}50}
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
52/// This is not whether the target supports Position Independent Code, but whether the -fPIC58/// This is not whether the target supports Position Independent Code, but whether the -fPIC
53/// C compiler argument is valid to Clang.59/// C compiler argument is valid to Clang.
54pub fn supports_fpic(target: std.Target) bool {60pub fn supports_fpic(target: std.Target) bool {
src/zig_llvm.cpp+11-6
...@@ -81,7 +81,7 @@ static const bool assertions_on = false;...@@ -81,7 +81,7 @@ static const bool assertions_on = false;
8181
82LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,82LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
83 const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,83 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,
85 const char *abi_name)85 const char *abi_name)
86{86{
87 std::optional<Reloc::Model> RM;87 std::optional<Reloc::Model> RM;
...@@ -258,7 +258,6 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi...@@ -258,7 +258,6 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
258 options->bin_filename? options->bin_filename : options->asm_filename);258 options->bin_filename? options->bin_filename : options->asm_filename);
259259
260 TargetMachine &target_machine = *reinterpret_cast<TargetMachine*>(targ_machine_ref);260 TargetMachine &target_machine = *reinterpret_cast<TargetMachine*>(targ_machine_ref);
261 target_machine.setO0WantsFastISel(true);
262261
263 Module &llvm_module = *unwrap(module_ref);262 Module &llvm_module = *unwrap(module_ref);
264263
...@@ -369,6 +368,12 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi...@@ -369,6 +368,12 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
369 }368 }
370 }369 }
371370
371 if (options.allow_fast_isel) {
372 target_machine.setO0WantsFastISel(true);
373 } else {
374 target_machine.setFastISel(false);
375 }
376
372 // Optimization phase377 // Optimization phase
373 module_pm.run(llvm_module, module_am);378 module_pm.run(llvm_module, module_am);
374379
...@@ -430,12 +435,12 @@ void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv) {...@@ -430,12 +435,12 @@ void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv) {
430 cl::ParseCommandLineOptions(argc, argv);435 cl::ParseCommandLineOptions(argc, argv);
431}436}
432437
433void ZigLLVMSetModulePICLevel(LLVMModuleRef module) {438void ZigLLVMSetModulePICLevel(LLVMModuleRef module, bool big) {
434 unwrap(module)->setPICLevel(PICLevel::Level::BigPIC);439 unwrap(module)->setPICLevel(big ? PICLevel::Level::BigPIC : PICLevel::Level::SmallPIC);
435}440}
436441
437void ZigLLVMSetModulePIELevel(LLVMModuleRef module) {442void ZigLLVMSetModulePIELevel(LLVMModuleRef module, bool large) {
438 unwrap(module)->setPIELevel(PIELevel::Level::Large);443 unwrap(module)->setPIELevel(large ? PIELevel::Level::Large : PIELevel::Level::Small);
439}444}
440445
441void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model) {446void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model) {
src/zig_llvm.h+4-3
...@@ -59,6 +59,7 @@ struct ZigLLVMEmitOptions {...@@ -59,6 +59,7 @@ struct ZigLLVMEmitOptions {
59 bool tsan;59 bool tsan;
60 bool sancov;60 bool sancov;
61 bool lto;61 bool lto;
62 bool allow_fast_isel;
62 const char *asm_filename;63 const char *asm_filename;
63 const char *bin_filename;64 const char *bin_filename;
64 const char *llvm_ir_filename;65 const char *llvm_ir_filename;
...@@ -77,7 +78,7 @@ enum ZigLLVMABIType {...@@ -77,7 +78,7 @@ enum ZigLLVMABIType {
7778
78ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,79ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
79 const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,80 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,
81 const char *abi_name);82 const char *abi_name);
8283
83ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);84ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);
...@@ -154,8 +155,8 @@ enum ZigLLVM_CallingConv {...@@ -154,8 +155,8 @@ enum ZigLLVM_CallingConv {
154 ZigLLVM_MaxID = 1023,155 ZigLLVM_MaxID = 1023,
155};156};
156157
157ZIG_EXTERN_C void ZigLLVMSetModulePICLevel(LLVMModuleRef module);158ZIG_EXTERN_C void ZigLLVMSetModulePICLevel(LLVMModuleRef module, bool big);
158ZIG_EXTERN_C void ZigLLVMSetModulePIELevel(LLVMModuleRef module);159ZIG_EXTERN_C void ZigLLVMSetModulePIELevel(LLVMModuleRef module, bool large);
159ZIG_EXTERN_C void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model);160ZIG_EXTERN_C void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model);
160161
161ZIG_EXTERN_C void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv);162ZIG_EXTERN_C void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv);