Skip to content

Commit ec919c3

Browse files
committed
LLVM: integrate param attrs with iterateParamTypes
This moves some logic from resolveLlvmFunction to updateFunc and takes advantage of the iteration we already do that takes into account C ABI lowering, making LLVM parameter attributes accurate for C ABI functions as well as our own unspecified calling convention. Related to #11498.
1 parent 43311e1 commit ec919c3

File tree

1 file changed

+22
-34
lines changed

1 file changed

+22
-34
lines changed

src/codegen/llvm.zig

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,6 @@ pub const Object = struct {
712712
.byval => {
713713
const param_ty = fn_info.param_types[it.zig_index - 1];
714714
const param = llvm_func.getParam(llvm_arg_i);
715-
llvm_arg_i += 1;
716715

717716
if (isByRef(param_ty)) {
718717
const alignment = param_ty.abiAlignment(target);
@@ -724,11 +723,33 @@ pub const Object = struct {
724723
try args.append(arg_ptr);
725724
} else {
726725
try args.append(param);
726+
727+
if (param_ty.isPtrAtRuntime()) {
728+
// TODO noalias attribute
729+
const ptr_info = param_ty.ptrInfo().data;
730+
if (!param_ty.isPtrLikeOptional() and !ptr_info.@"allowzero") {
731+
dg.addArgAttr(llvm_func, llvm_arg_i, "nonnull");
732+
}
733+
if (!ptr_info.mutable) {
734+
dg.addArgAttr(llvm_func, llvm_arg_i, "readonly");
735+
}
736+
if (ptr_info.@"align" != 0) {
737+
dg.addArgAttrInt(llvm_func, llvm_arg_i, "align", ptr_info.@"align");
738+
} else {
739+
dg.addArgAttrInt(llvm_func, llvm_arg_i, "align", ptr_info.pointee_type.abiAlignment(target));
740+
}
741+
}
727742
}
743+
llvm_arg_i += 1;
728744
},
729745
.byref => {
730746
const param_ty = fn_info.param_types[it.zig_index - 1];
731747
const param = llvm_func.getParam(llvm_arg_i);
748+
749+
dg.addArgAttr(llvm_func, llvm_arg_i, "nonnull");
750+
dg.addArgAttr(llvm_func, llvm_arg_i, "readonly");
751+
dg.addArgAttrInt(llvm_func, llvm_arg_i, "align", param_ty.abiAlignment(target));
752+
732753
llvm_arg_i += 1;
733754

734755
if (isByRef(param_ty)) {
@@ -2213,35 +2234,8 @@ pub const DeclGen = struct {
22132234
dg.addArgAttr(llvm_fn, @boolToInt(sret), "nonnull");
22142235
}
22152236

2216-
// Set parameter attributes.
22172237
switch (fn_info.cc) {
22182238
.Unspecified, .Inline => {
2219-
var llvm_param_i: c_uint = @as(c_uint, @boolToInt(sret)) + @boolToInt(err_return_tracing);
2220-
for (fn_info.param_types) |param_ty| {
2221-
if (!param_ty.hasRuntimeBitsIgnoreComptime()) continue;
2222-
2223-
// TODO: noalias attribute
2224-
2225-
if (isByRef(param_ty)) {
2226-
dg.addArgAttr(llvm_fn, llvm_param_i, "nonnull");
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-
}
2242-
}
2243-
llvm_param_i += 1;
2244-
}
22452239
llvm_fn.setFunctionCallConv(.Fast);
22462240
},
22472241
.Naked => {
@@ -2252,12 +2246,6 @@ pub const DeclGen = struct {
22522246
@panic("TODO: LLVM backend lower async function");
22532247
},
22542248
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.
22612249
llvm_fn.setFunctionCallConv(toLlvmCallConv(fn_info.cc, target));
22622250
},
22632251
}

0 commit comments

Comments
 (0)