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
Add support for code model selection #4318
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the patch. Just a few things to clean up, and then I'll be happy to merge this.
lib/std/builtin.zig
Outdated
pub const CodeModel = enum { | ||
Default, | ||
Tiny, | ||
Small, | ||
Kernel, | ||
Medium, | ||
Large, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New enums should follow #2101. This will be nice because you can use @tagName
above instead of the switch.
lib/std/build.zig
Outdated
@@ -1149,6 +1149,7 @@ pub const LibExeObjStep = struct { | |||
name_prefix: []const u8, | |||
filter: ?[]const u8, | |||
single_threaded: bool, | |||
code_model: ?builtin.CodeModel, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest:
code_model: ?builtin.CodeModel, | |
code_model: builtin.CodeModel = CodeModel.default, |
and then the code below should omit the -code-model
parameter when the value is default.
src/all_types.hpp
Outdated
LLVMCodeModel code_model; | ||
CodeModel want_code_model; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we only need 1 field here:
CodeModel code_model;
codegen_create
can default this to CodeModelDefault
. main.cpp can override it.
The code that generates builtin.zig can switch on the CodeModel (not the LLVM one).
detect_code_model
can be renamed to to_llvm_code_model
, and only called once, for createTargetMachine
.
The want_*
mechanisms don't apply to this parameter, since it has a default and there is no logic in the "detect" phase.
/// This data structure is used by the Zig language code generation and | ||
/// therefore must be kept in sync with the compiler implementation. | ||
pub const CodeModel = enum { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add your handy explanation to the doc comments?
/// The code model puts constraints on the location of symbols and the size of code and data.
/// The selection of a code model is a trade off on speed and restrictions that needs to be selected on a per application basis to meet its requirements.
/// A slightly more detailed explanation can be found in (for example) the [System V Application Binary Interface (x86_64)](https://github.com/hjl-tools/x86-psABI/wiki/x86-64-psABI-1.0.pdf) 3.5.1.
Thanks! |
This pull request allows the user to select the code model that is targeted through the
-code-model
compiler flag or theLibExeObjStep.code_model
field.It is exposed through
builtin.code_model
.Explanation:
In short the code model puts constraints on the location of symbols and the size of code and data.
The selection of a code model is a trade off on speed and restrictions that needs to be selected on a per application basis to meet its requirements.
A slightly more detailed explanation can be found in (for example) the System V Application Binary Interface (x86_64) 3.5.1.
Example:
An application compiled with the default amd64 code model (small) cannot be linked into negative address space because of the restrictions that it imposes.
Example for the linker error that would occur:
The same code targeting the large or kernel code models would be allowed to link.