Skip to content

Commit 43311e1

Browse files
committed
LLVM: add readonly, nonnull, align attributes to pointer params
1 parent 6d691d3 commit 43311e1

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/codegen/llvm.zig

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,16 +2214,31 @@ pub const DeclGen = struct {
22142214
}
22152215

22162216
// Set parameter attributes.
2217-
// TODO: more attributes. see codegen.cpp `make_fn_llvm_value`.
22182217
switch (fn_info.cc) {
22192218
.Unspecified, .Inline => {
22202219
var llvm_param_i: c_uint = @as(c_uint, @boolToInt(sret)) + @boolToInt(err_return_tracing);
22212220
for (fn_info.param_types) |param_ty| {
22222221
if (!param_ty.hasRuntimeBitsIgnoreComptime()) continue;
22232222

2223+
// TODO: noalias attribute
2224+
22242225
if (isByRef(param_ty)) {
22252226
dg.addArgAttr(llvm_fn, llvm_param_i, "nonnull");
2226-
// TODO readonly, noalias, align
2227+
dg.addArgAttr(llvm_fn, llvm_param_i, "readonly");
2228+
dg.addArgAttrInt(llvm_fn, llvm_param_i, "align", param_ty.abiAlignment(target));
2229+
} else if (param_ty.isPtrAtRuntime()) {
2230+
const ptr_info = param_ty.ptrInfo().data;
2231+
if (!param_ty.isPtrLikeOptional() and !ptr_info.@"allowzero") {
2232+
dg.addArgAttr(llvm_fn, llvm_param_i, "nonnull");
2233+
}
2234+
if (!ptr_info.mutable) {
2235+
dg.addArgAttr(llvm_fn, llvm_param_i, "readonly");
2236+
}
2237+
if (ptr_info.@"align" != 0) {
2238+
dg.addArgAttrInt(llvm_fn, llvm_param_i, "align", ptr_info.@"align");
2239+
} else {
2240+
dg.addArgAttrInt(llvm_fn, llvm_param_i, "align", ptr_info.pointee_type.abiAlignment(target));
2241+
}
22272242
}
22282243
llvm_param_i += 1;
22292244
}
@@ -2237,6 +2252,12 @@ pub const DeclGen = struct {
22372252
@panic("TODO: LLVM backend lower async function");
22382253
},
22392254
else => {
2255+
// TODO set attributes such as noalias, nonnull, readonly, and align
2256+
// Note that there is not a one to one correspondence between fn_info.param_types
2257+
// and llvm parameters due to C ABI lowering. This will need to involve
2258+
// iterateParamTypes which is currently happening over in updateFunc.
2259+
// Probably this whole "set parameter attributes" section of code should
2260+
// move there and integrate with this abstraction.
22402261
llvm_fn.setFunctionCallConv(toLlvmCallConv(fn_info.cc, target));
22412262
},
22422263
}
@@ -3705,6 +3726,10 @@ pub const DeclGen = struct {
37053726
return dg.addAttr(fn_val, param_index + 1, attr_name);
37063727
}
37073728

3729+
fn addArgAttrInt(dg: DeclGen, fn_val: *const llvm.Value, param_index: u32, attr_name: []const u8, int: u64) void {
3730+
return dg.addAttrInt(fn_val, param_index + 1, attr_name, int);
3731+
}
3732+
37083733
fn removeAttr(val: *const llvm.Value, index: llvm.AttributeIndex, name: []const u8) void {
37093734
const kind_id = llvm.getEnumAttributeKindForName(name.ptr, name.len);
37103735
assert(kind_id != 0);

0 commit comments

Comments
 (0)