diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-06-24 15:03:15 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-06-25 01:41:08 -0700 |
| commit | 9684947faae674e093dda4c81d94f51b9568369e (patch) | |
| tree | 2ea16a1a8187e467877fac1dc7d87ede9e1e0898 /src/Module.zig | |
| parent | 82ad66b2f2bd822e6d195768da9e5247ed4d78cc (diff) | |
| download | zig-9684947faae674e093dda4c81d94f51b9568369e.tar.gz zig-9684947faae674e093dda4c81d94f51b9568369e.zip | |
compiler: start moving safety-checks into backends
This actually used to be how it worked in stage1, and there was this
issue to change it: #2649
So this commit is a reversal to that idea. One motivation for that issue
was avoiding emitting the panic handler in compilations that do not have
any calls to panic. This commit only resolves the panic handler in the
event of a safety check function being emitted, so it does not have that
flaw.
The other reason given in that issue was for optimizations that elide
safety checks. It's yet to be determined whether that was a good idea or
not; this can get re-explored when we start adding optimization passes
to AIR.
This commit adds these AIR instructions, which are only emitted if
`backendSupportsFeature(.safety_checked_arithmetic)` is true:
* add_safe
* sub_safe
* mul_safe
It removes these nonsensical AIR instructions:
* addwrap_optimized
* subwrap_optimized
* mulwrap_optimized
The safety-checked arithmetic functions push the burden of invoking the
panic handler into the backend. This makes for a messier compiler
implementation, but it reduces the amount of AIR instructions emitted by
Sema, which reduces time spent in the secondary bottleneck of the
compiler. It also generates more compact LLVM IR, reducing time spent in
the primary bottleneck of the compiler.
Finally, it eliminates 1 stack allocation per safety-check which was
being used to store the resulting tuple. These allocations were going to
be annoying when combined with suspension points.
Diffstat (limited to 'src/Module.zig')
| -rw-r--r-- | src/Module.zig | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/Module.zig b/src/Module.zig index f88f047578..6d04b8f894 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -187,6 +187,40 @@ reference_table: std.AutoHashMapUnmanaged(Decl.Index, struct { src: LazySrcLoc, }) = .{}, +panic_messages: [PanicId.len]Decl.OptionalIndex = .{.none} ** PanicId.len, +panic_func_index: Fn.OptionalIndex = .none, +null_stack_trace: InternPool.Index = .none, + +pub const PanicId = enum { + unreach, + unwrap_null, + cast_to_null, + incorrect_alignment, + invalid_error_code, + cast_truncated_data, + negative_to_unsigned, + integer_overflow, + shl_overflow, + shr_overflow, + divide_by_zero, + exact_division_remainder, + inactive_union_field, + integer_part_out_of_bounds, + corrupt_switch, + shift_rhs_too_big, + invalid_enum_value, + sentinel_mismatch, + unwrap_error, + index_out_of_bounds, + start_index_greater_than_end, + for_len_mismatch, + memcpy_len_mismatch, + memcpy_alias, + noreturn_returned, + + pub const len = @typeInfo(PanicId).Enum.fields.len; +}; + pub const GlobalErrorSet = std.AutoArrayHashMapUnmanaged(InternPool.NullTerminatedString, void); pub const CImportError = struct { @@ -6651,6 +6685,14 @@ pub const Feature = enum { is_named_enum_value, error_set_has_value, field_reordering, + /// When this feature is supported, the backend supports the following AIR instructions: + /// * `Air.Inst.Tag.add_safe` + /// * `Air.Inst.Tag.sub_safe` + /// * `Air.Inst.Tag.mul_safe` + /// The motivation for this feature is that it makes AIR smaller, and makes it easier + /// to generate better machine code in the backends. All backends should migrate to + /// enabling this feature. + safety_checked_instructions, }; pub fn backendSupportsFeature(mod: Module, feature: Feature) bool { @@ -6665,6 +6707,7 @@ pub fn backendSupportsFeature(mod: Module, feature: Feature) bool { .is_named_enum_value => mod.comp.bin_file.options.use_llvm, .error_set_has_value => mod.comp.bin_file.options.use_llvm or mod.comp.bin_file.options.target.isWasm(), .field_reordering => mod.comp.bin_file.options.use_llvm, + .safety_checked_instructions => mod.comp.bin_file.options.use_llvm, }; } |
