Skip to content

Commit 1725619

Browse files
committed
librustc: Use builder for llvm attributes.
1 parent 74db699 commit 1725619

File tree

10 files changed

+181
-74
lines changed

10 files changed

+181
-74
lines changed

src/librustc/middle/trans/base.rs

+34-39
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,7 @@ pub fn decl_rust_fn(ccx: &CrateContext, fn_ty: ty::t, name: &str) -> ValueRef {
281281

282282
let llfn = decl_fn(ccx, name, llvm::CCallConv, llfty, output);
283283
let attrs = get_fn_llvm_attributes(ccx, fn_ty);
284-
for &(idx, attr) in attrs.iter() {
285-
unsafe {
286-
llvm::LLVMAddFunctionAttribute(llfn, idx as c_uint, attr);
287-
}
288-
}
284+
attrs.apply_llfn(llfn);
289285

290286
llfn
291287
}
@@ -962,7 +958,7 @@ pub fn invoke<'a>(
962958
llargs.as_slice(),
963959
normal_bcx.llbb,
964960
landing_pad,
965-
attributes.as_slice());
961+
Some(attributes));
966962
return (llresult, normal_bcx);
967963
} else {
968964
debug!("calling {} at {}", llfn, bcx.llbb);
@@ -975,7 +971,7 @@ pub fn invoke<'a>(
975971
None => debuginfo::clear_source_location(bcx.fcx)
976972
};
977973

978-
let llresult = Call(bcx, llfn, llargs.as_slice(), attributes.as_slice());
974+
let llresult = Call(bcx, llfn, llargs.as_slice(), Some(attributes));
979975
return (llresult, bcx);
980976
}
981977
}
@@ -1081,7 +1077,7 @@ pub fn call_lifetime_start(cx: &Block, ptr: ValueRef) {
10811077
let llsize = C_u64(ccx, machine::llsize_of_alloc(ccx, val_ty(ptr).element_type()));
10821078
let ptr = PointerCast(cx, ptr, Type::i8p(ccx));
10831079
let lifetime_start = ccx.get_intrinsic(&"llvm.lifetime.start");
1084-
Call(cx, lifetime_start, [llsize, ptr], []);
1080+
Call(cx, lifetime_start, [llsize, ptr], None);
10851081
}
10861082

10871083
pub fn call_lifetime_end(cx: &Block, ptr: ValueRef) {
@@ -1095,7 +1091,7 @@ pub fn call_lifetime_end(cx: &Block, ptr: ValueRef) {
10951091
let llsize = C_u64(ccx, machine::llsize_of_alloc(ccx, val_ty(ptr).element_type()));
10961092
let ptr = PointerCast(cx, ptr, Type::i8p(ccx));
10971093
let lifetime_end = ccx.get_intrinsic(&"llvm.lifetime.end");
1098-
Call(cx, lifetime_end, [llsize, ptr], []);
1094+
Call(cx, lifetime_end, [llsize, ptr], None);
10991095
}
11001096

11011097
pub fn call_memcpy(cx: &Block, dst: ValueRef, src: ValueRef, n_bytes: ValueRef, align: u32) {
@@ -1111,7 +1107,7 @@ pub fn call_memcpy(cx: &Block, dst: ValueRef, src: ValueRef, n_bytes: ValueRef,
11111107
let size = IntCast(cx, n_bytes, ccx.int_type);
11121108
let align = C_i32(ccx, align as i32);
11131109
let volatile = C_bool(ccx, false);
1114-
Call(cx, memcpy, [dst_ptr, src_ptr, size, align, volatile], []);
1110+
Call(cx, memcpy, [dst_ptr, src_ptr, size, align, volatile], None);
11151111
}
11161112

11171113
pub fn memcpy_ty(bcx: &Block, dst: ValueRef, src: ValueRef, t: ty::t) {
@@ -1156,7 +1152,7 @@ fn memzero(b: &Builder, llptr: ValueRef, ty: Type) {
11561152
let size = machine::llsize_of(ccx, ty);
11571153
let align = C_i32(ccx, llalign_of_min(ccx, ty) as i32);
11581154
let volatile = C_bool(ccx, false);
1159-
b.call(llintrinsicfn, [llptr, llzeroval, size, align, volatile], []);
1155+
b.call(llintrinsicfn, [llptr, llzeroval, size, align, volatile], None);
11601156
}
11611157

11621158
pub fn alloc_ty(bcx: &Block, t: ty::t, name: &str) -> ValueRef {
@@ -2040,7 +2036,7 @@ fn register_fn(ccx: &CrateContext,
20402036
}
20412037

20422038
pub fn get_fn_llvm_attributes(ccx: &CrateContext, fn_ty: ty::t)
2043-
-> Vec<(uint, u64)> {
2039+
-> llvm::AttrBuilder {
20442040
use middle::ty::{BrAnon, ReLateBound};
20452041

20462042
let (fn_sig, abi, has_env) = match ty::get(fn_ty).sty {
@@ -2056,31 +2052,30 @@ pub fn get_fn_llvm_attributes(ccx: &CrateContext, fn_ty: ty::t)
20562052
_ => fail!("expected closure or function.")
20572053
};
20582054

2055+
// Since index 0 is the return value of the llvm func, we start
2056+
// at either 1 or 2 depending on whether there's an env slot or not
2057+
let mut first_arg_offset = if has_env { 2 } else { 1 };
2058+
let mut attrs = llvm::AttrBuilder::new();
2059+
let ret_ty = fn_sig.output;
2060+
20592061
// These have an odd calling convention, so we skip them for now.
20602062
//
20612063
// FIXME(pcwalton): We don't have to skip them; just untuple the result.
20622064
if abi == RustCall {
2063-
return Vec::new()
2065+
return attrs;
20642066
}
20652067

2066-
// Since index 0 is the return value of the llvm func, we start
2067-
// at either 1 or 2 depending on whether there's an env slot or not
2068-
let mut first_arg_offset = if has_env { 2 } else { 1 };
2069-
let mut attrs = Vec::new();
2070-
let ret_ty = fn_sig.output;
2071-
20722068
// A function pointer is called without the declaration
20732069
// available, so we have to apply any attributes with ABI
20742070
// implications directly to the call instruction. Right now,
20752071
// the only attribute we need to worry about is `sret`.
20762072
if type_of::return_uses_outptr(ccx, ret_ty) {
2077-
attrs.push((1, llvm::StructRetAttribute as u64));
2078-
20792073
// The outptr can be noalias and nocapture because it's entirely
20802074
// invisible to the program. We can also mark it as nonnull
2081-
attrs.push((1, llvm::NoAliasAttribute as u64));
2082-
attrs.push((1, llvm::NoCaptureAttribute as u64));
2083-
attrs.push((1, llvm::NonNullAttribute as u64));
2075+
attrs.arg(1, llvm::StructRetAttribute)
2076+
.arg(1, llvm::NoAliasAttribute)
2077+
.arg(1, llvm::NoCaptureAttribute)
2078+
.arg(1, llvm::NonNullAttribute);
20842079

20852080
// Add one more since there's an outptr
20862081
first_arg_offset += 1;
@@ -2094,7 +2089,7 @@ pub fn get_fn_llvm_attributes(ccx: &CrateContext, fn_ty: ty::t)
20942089
ty::ty_str | ty::ty_vec(..) | ty::ty_trait(..) => true, _ => false
20952090
} => {}
20962091
ty::ty_uniq(_) => {
2097-
attrs.push((llvm::ReturnIndex as uint, llvm::NoAliasAttribute as u64));
2092+
attrs.ret(llvm::NoAliasAttribute);
20982093
}
20992094
_ => {}
21002095
}
@@ -2107,14 +2102,14 @@ pub fn get_fn_llvm_attributes(ccx: &CrateContext, fn_ty: ty::t)
21072102
ty::ty_str | ty::ty_vec(..) | ty::ty_trait(..) => true, _ => false
21082103
} => {}
21092104
ty::ty_uniq(_) | ty::ty_rptr(_, _) => {
2110-
attrs.push((llvm::ReturnIndex as uint, llvm::NonNullAttribute as u64));
2105+
attrs.ret(llvm::NonNullAttribute);
21112106
}
21122107
_ => {}
21132108
}
21142109

21152110
match ty::get(ret_ty).sty {
21162111
ty::ty_bool => {
2117-
attrs.push((llvm::ReturnIndex as uint, llvm::ZExtAttribute as u64));
2112+
attrs.ret(llvm::ZExtAttribute);
21182113
}
21192114
_ => {}
21202115
}
@@ -2127,41 +2122,41 @@ pub fn get_fn_llvm_attributes(ccx: &CrateContext, fn_ty: ty::t)
21272122
// For non-immediate arguments the callee gets its own copy of
21282123
// the value on the stack, so there are no aliases. It's also
21292124
// program-invisible so can't possibly capture
2130-
attrs.push((idx, llvm::NoAliasAttribute as u64));
2131-
attrs.push((idx, llvm::NoCaptureAttribute as u64));
2132-
attrs.push((idx, llvm::NonNullAttribute as u64));
2125+
attrs.arg(idx, llvm::NoAliasAttribute)
2126+
.arg(idx, llvm::NoCaptureAttribute)
2127+
.arg(idx, llvm::NonNullAttribute);
21332128
}
21342129
ty::ty_bool => {
2135-
attrs.push((idx, llvm::ZExtAttribute as u64));
2130+
attrs.arg(idx, llvm::ZExtAttribute);
21362131
}
21372132
// `~` pointer parameters never alias because ownership is transferred
21382133
ty::ty_uniq(_) => {
2139-
attrs.push((idx, llvm::NoAliasAttribute as u64));
2140-
attrs.push((idx, llvm::NonNullAttribute as u64));
2134+
attrs.arg(idx, llvm::NoAliasAttribute)
2135+
.arg(idx, llvm::NonNullAttribute);
21412136
}
21422137
// `&mut` pointer parameters never alias other parameters, or mutable global data
21432138
// `&` pointer parameters never alias either (for LLVM's purposes) as long as the
21442139
// interior is safe
21452140
ty::ty_rptr(b, mt) if mt.mutbl == ast::MutMutable ||
21462141
!ty::type_contents(ccx.tcx(), mt.ty).interior_unsafe() => {
2147-
attrs.push((idx, llvm::NoAliasAttribute as u64));
2148-
attrs.push((idx, llvm::NonNullAttribute as u64));
2142+
attrs.arg(idx, llvm::NoAliasAttribute)
2143+
.arg(idx, llvm::NonNullAttribute);
21492144
match b {
21502145
ReLateBound(_, BrAnon(_)) => {
2151-
attrs.push((idx, llvm::NoCaptureAttribute as u64));
2146+
attrs.arg(idx, llvm::NoCaptureAttribute);
21522147
}
21532148
_ => {}
21542149
}
21552150
}
21562151
// When a reference in an argument has no named lifetime, it's impossible for that
21572152
// reference to escape this function (returned or stored beyond the call by a closure).
21582153
ty::ty_rptr(ReLateBound(_, BrAnon(_)), _) => {
2159-
attrs.push((idx, llvm::NoCaptureAttribute as u64));
2160-
attrs.push((idx, llvm::NonNullAttribute as u64));
2154+
attrs.arg(idx, llvm::NoCaptureAttribute)
2155+
.arg(idx, llvm::NonNullAttribute);
21612156
}
21622157
// & pointer parameters are never null
21632158
ty::ty_rptr(_, _) => {
2164-
attrs.push((idx, llvm::NonNullAttribute as u64));
2159+
attrs.arg(idx, llvm::NonNullAttribute);
21652160
}
21662161
_ => ()
21672162
}

src/librustc/middle/trans/build.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#![allow(non_snake_case_functions)]
1313

1414
use llvm;
15-
use llvm::{CallConv, AtomicBinOp, AtomicOrdering, AsmDialect};
15+
use llvm::{CallConv, AtomicBinOp, AtomicOrdering, AsmDialect, AttrBuilder};
1616
use llvm::{Opcode, IntPredicate, RealPredicate};
1717
use llvm::{ValueRef, BasicBlockRef};
1818
use middle::trans::common::*;
@@ -113,7 +113,7 @@ pub fn Invoke(cx: &Block,
113113
args: &[ValueRef],
114114
then: BasicBlockRef,
115115
catch: BasicBlockRef,
116-
attributes: &[(uint, u64)])
116+
attributes: Option<AttrBuilder>)
117117
-> ValueRef {
118118
if cx.unreachable.get() {
119119
return C_null(Type::i8(cx.ccx()));
@@ -681,13 +681,13 @@ pub fn InlineAsmCall(cx: &Block, asm: *const c_char, cons: *const c_char,
681681
}
682682

683683
pub fn Call(cx: &Block, fn_: ValueRef, args: &[ValueRef],
684-
attributes: &[(uint, u64)]) -> ValueRef {
684+
attributes: Option<AttrBuilder>) -> ValueRef {
685685
if cx.unreachable.get() { return _UndefReturn(cx, fn_); }
686686
B(cx).call(fn_, args, attributes)
687687
}
688688

689689
pub fn CallWithConv(cx: &Block, fn_: ValueRef, args: &[ValueRef], conv: CallConv,
690-
attributes: &[(uint, u64)]) -> ValueRef {
690+
attributes: Option<AttrBuilder>) -> ValueRef {
691691
if cx.unreachable.get() { return _UndefReturn(cx, fn_); }
692692
B(cx).call_with_conv(fn_, args, conv, attributes)
693693
}

src/librustc/middle/trans/builder.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![allow(dead_code)] // FFI wrappers
1212

1313
use llvm;
14-
use llvm::{CallConv, AtomicBinOp, AtomicOrdering, AsmDialect};
14+
use llvm::{CallConv, AtomicBinOp, AtomicOrdering, AsmDialect, AttrBuilder};
1515
use llvm::{Opcode, IntPredicate, RealPredicate, False};
1616
use llvm::{ValueRef, BasicBlockRef, BuilderRef, ModuleRef};
1717
use middle::trans::base;
@@ -155,7 +155,7 @@ impl<'a> Builder<'a> {
155155
args: &[ValueRef],
156156
then: BasicBlockRef,
157157
catch: BasicBlockRef,
158-
attributes: &[(uint, u64)])
158+
attributes: Option<AttrBuilder>)
159159
-> ValueRef {
160160
self.count_insn("invoke");
161161

@@ -174,8 +174,9 @@ impl<'a> Builder<'a> {
174174
then,
175175
catch,
176176
noname());
177-
for &(idx, attr) in attributes.iter() {
178-
llvm::LLVMAddCallSiteAttribute(v, idx as c_uint, attr);
177+
match attributes {
178+
Some(a) => a.apply_callsite(v),
179+
None => {}
179180
}
180181
v
181182
}
@@ -777,7 +778,7 @@ impl<'a> Builder<'a> {
777778
c, noname(), False, False)
778779
}
779780
});
780-
self.call(asm, [], []);
781+
self.call(asm, [], None);
781782
}
782783
}
783784

@@ -802,12 +803,12 @@ impl<'a> Builder<'a> {
802803
unsafe {
803804
let v = llvm::LLVMInlineAsm(
804805
fty.to_ref(), asm, cons, volatile, alignstack, dia as c_uint);
805-
self.call(v, inputs, [])
806+
self.call(v, inputs, None)
806807
}
807808
}
808809

809810
pub fn call(&self, llfn: ValueRef, args: &[ValueRef],
810-
attributes: &[(uint, u64)]) -> ValueRef {
811+
attributes: Option<AttrBuilder>) -> ValueRef {
811812
self.count_insn("call");
812813

813814
debug!("Call {} with args ({})",
@@ -820,15 +821,16 @@ impl<'a> Builder<'a> {
820821
unsafe {
821822
let v = llvm::LLVMBuildCall(self.llbuilder, llfn, args.as_ptr(),
822823
args.len() as c_uint, noname());
823-
for &(idx, attr) in attributes.iter() {
824-
llvm::LLVMAddCallSiteAttribute(v, idx as c_uint, attr);
824+
match attributes {
825+
Some(a) => a.apply_callsite(v),
826+
None => {}
825827
}
826828
v
827829
}
828830
}
829831

830832
pub fn call_with_conv(&self, llfn: ValueRef, args: &[ValueRef],
831-
conv: CallConv, attributes: &[(uint, u64)]) -> ValueRef {
833+
conv: CallConv, attributes: Option<AttrBuilder>) -> ValueRef {
832834
self.count_insn("callwithconv");
833835
let v = self.call(llfn, args, attributes);
834836
llvm::SetInstructionCallConv(v, conv);

src/librustc/middle/trans/closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ pub fn get_wrapper_for_bare_fn(ccx: &CrateContext,
588588
}
589589
llargs.extend(args.iter().map(|arg| arg.val));
590590

591-
let retval = Call(bcx, fn_ptr, llargs.as_slice(), []);
591+
let retval = Call(bcx, fn_ptr, llargs.as_slice(), None);
592592
if type_is_zero_size(ccx, f.sig.output) || fcx.llretptr.get().is_some() {
593593
RetVoid(bcx);
594594
} else {

src/librustc/middle/trans/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ fn trans_index<'a>(bcx: &'a Block<'a>,
553553
let expected = Call(bcx,
554554
expect,
555555
[bounds_check, C_bool(ccx, false)],
556-
[]);
556+
None);
557557
bcx = with_cond(bcx, expected, |bcx| {
558558
controlflow::trans_fail_bounds_check(bcx,
559559
index_expr.span,

src/librustc/middle/trans/foreign.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -382,21 +382,21 @@ pub fn trans_native_call<'a>(
382382

383383
// A function pointer is called without the declaration available, so we have to apply
384384
// any attributes with ABI implications directly to the call instruction.
385-
let mut attrs = Vec::new();
385+
let mut attrs = llvm::AttrBuilder::new();
386386

387387
// Add attributes that are always applicable, independent of the concrete foreign ABI
388388
if fn_type.ret_ty.is_indirect() {
389389
// The outptr can be noalias and nocapture because it's entirely
390390
// invisible to the program. We can also mark it as nonnull
391-
attrs.push((1, llvm::NoAliasAttribute as u64));
392-
attrs.push((1, llvm::NoCaptureAttribute as u64));
393-
attrs.push((1, llvm::NonNullAttribute as u64));
391+
attrs.arg(1, llvm::NoAliasAttribute)
392+
.arg(1, llvm::NoCaptureAttribute)
393+
.arg(1, llvm::NonNullAttribute);
394394
};
395395

396396
// Add attributes that depend on the concrete foreign ABI
397397
let mut arg_idx = if fn_type.ret_ty.is_indirect() { 1 } else { 0 };
398398
match fn_type.ret_ty.attr {
399-
Some(attr) => attrs.push((arg_idx, attr as u64)),
399+
Some(attr) => { attrs.arg(arg_idx, attr); },
400400
_ => ()
401401
}
402402

@@ -409,7 +409,7 @@ pub fn trans_native_call<'a>(
409409
if arg_ty.pad.is_some() { arg_idx += 1; }
410410

411411
match arg_ty.attr {
412-
Some(attr) => attrs.push((arg_idx, attr as u64)),
412+
Some(attr) => { attrs.arg(arg_idx, attr); },
413413
_ => {}
414414
}
415415

@@ -420,7 +420,7 @@ pub fn trans_native_call<'a>(
420420
llfn,
421421
llargs_foreign.as_slice(),
422422
cc,
423-
attrs.as_slice());
423+
Some(attrs));
424424

425425
// If the function we just called does not use an outpointer,
426426
// store the result into the rust outpointer. Cast the outpointer
@@ -762,7 +762,7 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: &CrateContext,
762762
// Perform the call itself
763763
debug!("calling llrustfn = {}, t = {}", ccx.tn.val_to_string(llrustfn), t.repr(ccx.tcx()));
764764
let attributes = base::get_fn_llvm_attributes(ccx, t);
765-
let llrust_ret_val = builder.call(llrustfn, llrust_args.as_slice(), attributes.as_slice());
765+
let llrust_ret_val = builder.call(llrustfn, llrust_args.as_slice(), Some(attributes));
766766

767767
// Get the return value where the foreign fn expects it.
768768
let llforeign_ret_ty = match tys.fn_ty.ret_ty.cast {

0 commit comments

Comments
 (0)