Skip to content

Commit

Permalink
add sret + noalias to the out pointer parameter
Browse files Browse the repository at this point in the history
This brings Rust in line with how `clang` handles return pointers.

Example:

    pub fn bar() -> [uint, .. 8] {
        let a = [0, .. 8];
        a
    }

Before:

    ; Function Attrs: nounwind uwtable
    define void @_ZN3bar17ha4635c6f704bfa334v0.0E([8 x i64]* nocapture, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #1 {
    "function top level":
      %a = alloca [8 x i64], align 8
      %2 = bitcast [8 x i64]* %a to i8*
      call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 64, i32 8, i1 false)
      %3 = bitcast [8 x i64]* %0 to i8*
      call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* %2, i64 64, i32 8, i1 false)
      ret void
    }

After:

    ; Function Attrs: nounwind uwtable
    define void @_ZN3bar17ha4635c6f704bfa334v0.0E([8 x i64]* noalias nocapture sret, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #1 {
    "function top level":
      %2 = bitcast [8 x i64]* %0 to i8*
      call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 64, i32 8, i1 false)
      ret void
    }

Closes #9072
Closes #7298
  • Loading branch information
thestinger committed Sep 10, 2013
1 parent 917d3c2 commit a543ed6
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,18 @@ pub fn new_fn_ctxt_w_id(ccx: @mut CrateContext,
}
};
let uses_outptr = type_of::return_uses_outptr(ccx.tcx, substd_output_type);

// The out pointer will never alias with any other pointers, as the object only exists at a
// language level after the call. It can also be tagged with SRet to indicate that it is
// guaranteed to point to a usable block of memory for the type.
if uses_outptr {
unsafe {
let outptr = llvm::LLVMGetParam(llfndecl, 0);
llvm::LLVMAddAttribute(outptr, lib::llvm::StructRetAttribute as c_uint);
llvm::LLVMAddAttribute(outptr, lib::llvm::NoAliasAttribute as c_uint);
}
}

let debug_context = debuginfo::create_function_debug_context(ccx, id, param_substs, llfndecl);

let fcx = @mut FunctionContext {
Expand Down

8 comments on commit a543ed6

@bors
Copy link
Contributor

@bors bors commented on a543ed6 Sep 10, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on a543ed6 Sep 10, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging thestinger/rust/outptr = a543ed6 into auto

@bors
Copy link
Contributor

@bors bors commented on a543ed6 Sep 10, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thestinger/rust/outptr = a543ed6 merged ok, testing candidate = 4e895d1d

@bors
Copy link
Contributor

@bors bors commented on a543ed6 Sep 10, 2013

@bors
Copy link
Contributor

@bors bors commented on a543ed6 Sep 10, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on a543ed6 Sep 10, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging thestinger/rust/outptr = a543ed6 into auto

@bors
Copy link
Contributor

@bors bors commented on a543ed6 Sep 10, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thestinger/rust/outptr = a543ed6 merged ok, testing candidate = c5247fda

@bors
Copy link
Contributor

@bors bors commented on a543ed6 Sep 10, 2013

Please sign in to comment.