Skip to content

Commit

Permalink
Merge branch 'main' into fix/replace_panic_with_internal_error
Browse files Browse the repository at this point in the history
  • Loading branch information
eiei114 authored Jun 28, 2023
2 parents feec075 + fd5616d commit 8e28571
Show file tree
Hide file tree
Showing 35 changed files with 440 additions and 289 deletions.
14 changes: 0 additions & 14 deletions crates/cli/tests/fixtures/multi-dep-str/platform/host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@ const testing = std.testing;
const expectEqual = testing.expectEqual;
const expect = testing.expect;

comptime {
// This is a workaround for https://github.com/ziglang/zig/issues/8218
// which is only necessary on macOS.
//
// Once that issue is fixed, we can undo the changes in
// 177cf12e0555147faa4d436e52fc15175c2c4ff0 and go back to passing
// -fcompiler-rt in link.rs instead of doing this. Note that this
// workaround is present in many host.zig files, so make sure to undo
// it everywhere!
if (builtin.os.tag == .macos) {
_ = @import("compiler_rt");
}
}

const mem = std.mem;
const Allocator = mem.Allocator;

Expand Down
14 changes: 0 additions & 14 deletions crates/cli/tests/fixtures/multi-dep-thunk/platform/host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@ const testing = std.testing;
const expectEqual = testing.expectEqual;
const expect = testing.expect;

comptime {
// This is a workaround for https://github.com/ziglang/zig/issues/8218
// which is only necessary on macOS.
//
// Once that issue is fixed, we can undo the changes in
// 177cf12e0555147faa4d436e52fc15175c2c4ff0 and go back to passing
// -fcompiler-rt in link.rs instead of doing this. Note that this
// workaround is present in many host.zig files, so make sure to undo
// it everywhere!
if (builtin.os.tag == .macos) {
_ = @import("compiler_rt");
}
}

const mem = std.mem;
const Allocator = mem.Allocator;

Expand Down
14 changes: 0 additions & 14 deletions crates/cli/tests/fixtures/packages/platform/host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@ const testing = std.testing;
const expectEqual = testing.expectEqual;
const expect = testing.expect;

comptime {
// This is a workaround for https://github.com/ziglang/zig/issues/8218
// which is only necessary on macOS.
//
// Once that issue is fixed, we can undo the changes in
// 177cf12e0555147faa4d436e52fc15175c2c4ff0 and go back to passing
// -fcompiler-rt in link.rs instead of doing this. Note that this
// workaround is present in many host.zig files, so make sure to undo
// it everywhere!
if (builtin.os.tag == .macos) {
_ = @import("compiler_rt");
}
}

const mem = std.mem;
const Allocator = mem.Allocator;

Expand Down
14 changes: 0 additions & 14 deletions crates/cli_testing_examples/algorithms/fibonacci-platform/host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,6 @@ const expectEqual = testing.expectEqual;
const expect = testing.expect;
const maxInt = std.math.maxInt;

comptime {
// This is a workaround for https://github.com/ziglang/zig/issues/8218
// which is only necessary on macOS.
//
// Once that issue is fixed, we can undo the changes in
// 177cf12e0555147faa4d436e52fc15175c2c4ff0 and go back to passing
// -fcompiler-rt in link.rs instead of doing this. Note that this
// workaround is present in many host.zig files, so make sure to undo
// it everywhere!
if (builtin.os.tag == .macos) {
_ = @import("compiler_rt");
}
}

const mem = std.mem;
const Allocator = mem.Allocator;

Expand Down
14 changes: 0 additions & 14 deletions crates/cli_testing_examples/algorithms/quicksort-platform/host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@ const testing = std.testing;
const expectEqual = testing.expectEqual;
const expect = testing.expect;

comptime {
// This is a workaround for https://github.com/ziglang/zig/issues/8218
// which is only necessary on macOS.
//
// Once that issue is fixed, we can undo the changes in
// 177cf12e0555147faa4d436e52fc15175c2c4ff0 and go back to passing
// -fcompiler-rt in link.rs instead of doing this. Note that this
// workaround is present in many host.zig files, so make sure to undo
// it everywhere!
if (builtin.os.tag == .macos) {
_ = @import("compiler_rt");
}
}

const mem = std.mem;
const Allocator = mem.Allocator;

Expand Down
14 changes: 0 additions & 14 deletions crates/cli_testing_examples/benchmarks/platform/host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,6 @@ const expectEqual = testing.expectEqual;
const expect = testing.expect;
const maxInt = std.math.maxInt;

comptime {
// This is a workaround for https://github.com/ziglang/zig/issues/8218
// which is only necessary on macOS.
//
// Once that issue is fixed, we can undo the changes in
// 177cf12e0555147faa4d436e52fc15175c2c4ff0 and go back to passing
// -fcompiler-rt in link.rs instead of doing this. Note that this
// workaround is present in many host.zig files, so make sure to undo
// it everywhere!
if (builtin.os.tag == .macos) {
_ = @import("compiler_rt");
}
}

const mem = std.mem;
const Allocator = mem.Allocator;

Expand Down
14 changes: 0 additions & 14 deletions crates/cli_testing_examples/expects/zig-platform/host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@ const testing = std.testing;
const expectEqual = testing.expectEqual;
const expect = testing.expect;

comptime {
// This is a workaround for https://github.com/ziglang/zig/issues/8218
// which is only necessary on macOS.
//
// Once that issue is fixed, we can undo the changes in
// 177cf12e0555147faa4d436e52fc15175c2c4ff0 and go back to passing
// -fcompiler-rt in link.rs instead of doing this. Note that this
// workaround is present in many host.zig files, so make sure to undo
// it everywhere!
if (builtin.os.tag == .macos) {
_ = @import("compiler_rt");
}
}

const Align = 2 * @alignOf(usize);
extern fn malloc(size: usize) callconv(.C) ?*align(Align) anyopaque;
extern fn realloc(c_ptr: [*]align(Align) u8, size: usize) callconv(.C) ?*anyopaque;
Expand Down
95 changes: 1 addition & 94 deletions crates/compiler/build/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn find_wasi_libc_path() -> PathBuf {
internal_error!("cannot find `wasi-libc.a`")
}

#[cfg(all(unix, not(target_os = "macos")))]
#[cfg(unix)]
#[allow(clippy::too_many_arguments)]
pub fn build_zig_host_native(
env_path: &str,
Expand Down Expand Up @@ -257,99 +257,6 @@ pub fn build_zig_host_native(
zig_cmd
}

#[cfg(target_os = "macos")]
#[allow(clippy::too_many_arguments)]
pub fn build_zig_host_native(
env_path: &str,
env_home: &str,
emit_bin: &str,
zig_host_src: &str,
_target: &str,
opt_level: OptLevel,
shared_lib_path: Option<&Path>,
builtins_host_path: &Path,
// For compatibility with the non-macOS def above. Keep these in sync.
) -> Command {
use serde_json::Value;

// Run `zig env` to find the location of zig's std/ directory
let zig_env_output = zig().args(["env"]).output().unwrap();

let zig_env_json = if zig_env_output.status.success() {
std::str::from_utf8(&zig_env_output.stdout).unwrap_or_else(|utf8_err| {
internal_error!(
"`zig env` failed; its stderr output was invalid utf8 ({:?})",
utf8_err
);
})
} else {
match std::str::from_utf8(&zig_env_output.stderr) {
Ok(stderr) => internal_error!("`zig env` failed - stderr output was: {:?}", stderr),
Err(utf8_err) => internal_error!(
"`zig env` failed; its stderr output was invalid utf8 ({:?})",
utf8_err
),
}
};

let mut zig_compiler_rt_path = match serde_json::from_str(zig_env_json) {
Ok(Value::Object(map)) => match map.get("std_dir") {
Some(Value::String(std_dir)) => PathBuf::from(Path::new(std_dir)),
_ => {
internal_error!("Expected JSON containing a `std_dir` String field from `zig env`, but got: {:?}", zig_env_json);
}
},
_ => {
internal_error!(
"Expected JSON containing a `std_dir` field from `zig env`, but got: {:?}",
zig_env_json
);
}
};

zig_compiler_rt_path.push("special");
zig_compiler_rt_path.push("compiler_rt.zig");

let mut zig_cmd = zig();
zig_cmd
.env_clear()
.env("PATH", env_path)
.env("HOME", env_home);
if let Some(shared_lib_path) = shared_lib_path {
zig_cmd.args([
"build-exe",
"-fPIE",
shared_lib_path.to_str().unwrap(),
builtins_host_path.to_str().unwrap(),
]);
} else {
zig_cmd.args(["build-obj"]);
}
zig_cmd.args([
zig_host_src,
&format!("-femit-bin={}", emit_bin),
"--pkg-begin",
"glue",
find_zig_glue_path().to_str().unwrap(),
"--pkg-end",
// include the zig runtime
"--pkg-begin",
"compiler_rt",
zig_compiler_rt_path.to_str().unwrap(),
"--pkg-end",
// include libc
"--library",
"c",
]);
if matches!(opt_level, OptLevel::Optimize) {
zig_cmd.args(["-O", "ReleaseSafe"]);
} else if matches!(opt_level, OptLevel::Size) {
zig_cmd.args(["-O", "ReleaseSmall"]);
}

zig_cmd
}

pub fn build_zig_host_wasm32(
env_path: &str,
env_home: &str,
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/build/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ fn gen_from_mono_module_llvm<'a>(

let kind_id = Attribute::get_named_enum_kind_id("alwaysinline");
debug_assert!(kind_id > 0);
let enum_attr = context.create_enum_attribute(kind_id, 1);
let enum_attr = context.create_enum_attribute(kind_id, 0);

for function in module.get_functions() {
let name = function.get_name().to_str().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/build/src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ pub fn target_zig_str(target: &Triple) -> &'static str {
architecture: Architecture::X86_64,
operating_system: OperatingSystem::Darwin,
..
} => "x86_64-apple-darwin",
} => "x86_64-macos-gnu",
Triple {
architecture: Architecture::Aarch64(_),
operating_system: OperatingSystem::Darwin,
..
} => "aarch64-apple-darwin",
} => "aarch64-macos-gnu",
_ => internal_error!("TODO gracefully handle unsupported target: {:?}", target),
}
}
Expand Down
45 changes: 45 additions & 0 deletions crates/compiler/builtins/bitcode/src/compiler_rt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const v2u64 = @Vector(2, u64);
// Export it as weak incase it is already linked in by something else.
comptime {
@export(__muloti4, .{ .name = "__muloti4", .linkage = .Weak });
@export(__lshrti3, .{ .name = "__lshrti3", .linkage = .Weak });
if (want_windows_v2u64_abi) {
@export(__divti3_windows_x86_64, .{ .name = "__divti3", .linkage = .Weak });
@export(__modti3_windows_x86_64, .{ .name = "__modti3", .linkage = .Weak });
Expand Down Expand Up @@ -440,3 +441,47 @@ pub inline fn floatFractionalBits(comptime T: type) comptime_int {
else => @compileError("unknown floating point type " ++ @typeName(T)),
};
}

pub fn __lshrti3(a: i128, b: i32) callconv(.C) i128 {
return lshrXi3(i128, a, b);
}

// Logical shift right: shift in 0 from left to right
// Precondition: 0 <= b < T.bit_count
inline fn lshrXi3(comptime T: type, a: T, b: i32) T {
const word_t = HalveInt(T, false);
const S = std.math.Log2Int(word_t.HalfT);

const input = word_t{ .all = a };
var output: word_t = undefined;

if (b >= word_t.bits) {
output.s.high = 0;
output.s.low = input.s.high >> @intCast(S, b - word_t.bits);
} else if (b == 0) {
return a;
} else {
output.s.high = input.s.high >> @intCast(S, b);
output.s.low = input.s.high << @intCast(S, word_t.bits - b);
output.s.low |= input.s.low >> @intCast(S, b);
}

return output.all;
}

/// Allows to access underlying bits as two equally sized lower and higher
/// signed or unsigned integers.
fn HalveInt(comptime T: type, comptime signed_half: bool) type {
return extern union {
pub const bits = @divExact(@typeInfo(T).Int.bits, 2);
pub const HalfTU = std.meta.Int(.unsigned, bits);
pub const HalfTS = std.meta.Int(.signed, bits);
pub const HalfT = if (signed_half) HalfTS else HalfTU;

all: T,
s: if (native_endian == .Little)
extern struct { low: HalfT, high: HalfT }
else
extern struct { high: HalfT, low: HalfT },
};
}
8 changes: 4 additions & 4 deletions crates/compiler/gen_llvm/src/llvm/bitcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ fn build_transform_caller_help<'a, 'ctx>(

let kind_id = Attribute::get_named_enum_kind_id("alwaysinline");
debug_assert!(kind_id > 0);
let attr = env.context.create_enum_attribute(kind_id, 1);
let attr = env.context.create_enum_attribute(kind_id, 0);
function_value.add_attribute(AttributeLoc::Function, attr);

let entry = env.context.append_basic_block(function_value, "entry");
Expand Down Expand Up @@ -408,7 +408,7 @@ fn build_rc_wrapper<'a, 'ctx>(

let kind_id = Attribute::get_named_enum_kind_id("alwaysinline");
debug_assert!(kind_id > 0);
let attr = env.context.create_enum_attribute(kind_id, 1);
let attr = env.context.create_enum_attribute(kind_id, 0);
function_value.add_attribute(AttributeLoc::Function, attr);

let entry = env.context.append_basic_block(function_value, "entry");
Expand Down Expand Up @@ -497,7 +497,7 @@ pub fn build_eq_wrapper<'a, 'ctx>(

let kind_id = Attribute::get_named_enum_kind_id("alwaysinline");
debug_assert!(kind_id > 0);
let attr = env.context.create_enum_attribute(kind_id, 1);
let attr = env.context.create_enum_attribute(kind_id, 0);
function_value.add_attribute(AttributeLoc::Function, attr);

let entry = env.context.append_basic_block(function_value, "entry");
Expand Down Expand Up @@ -598,7 +598,7 @@ pub fn build_compare_wrapper<'a, 'ctx>(

let kind_id = Attribute::get_named_enum_kind_id("alwaysinline");
debug_assert!(kind_id > 0);
let attr = env.context.create_enum_attribute(kind_id, 1);
let attr = env.context.create_enum_attribute(kind_id, 0);
function_value.add_attribute(AttributeLoc::Function, attr);

let entry = env.context.append_basic_block(function_value, "entry");
Expand Down
Loading

0 comments on commit 8e28571

Please sign in to comment.