(original) (raw)


On 11/03/2017 07:20 PM, Alexandre Isoard via llvm-dev wrote:
Hello,

I am not sure about the semantic (if any) of returns\_twice and noreturn attributes.

int fork() \_\_attribute\_\_((returns\_twice));
void join(int) \_\_attribute\_\_((noreturn));

int f(int n) {
int t = fork();
n++;
if (t != 0)
join(t);
return n;
}

Produces the following LLVM IR:

; Function Attrs: nounwind uwtable
define i32 @f(i32 %n) local\_unnamed\_addr #0 {
entry:
%call = call i32 (...) @fork() #3
%cmp = icmp eq i32 %call, 0
br i1 %cmp, label %if.end, label %if.then

if.then: ; preds = %entry
call void @join(i32 %call) #4
unreachable

if.end: ; preds = %entry
%inc = add nsw i32 %n, 1
ret i32 %inc
}

; Function Attrs: returns\_twice
declare i32 @fork(...) local\_unnamed\_addr #1

; Function Attrs: noreturn
declare void @join(i32) local\_unnamed\_addr #2

Where the n++ has been moved after the if, is that legal?

Why wouldn't it be?


Also, technically, f could also returns\_twice or noreturn (depending on the return values of fork).

So my question is: do they have semantic or they are only "clues" for heuristic purposes?

They have semantics. returns\_twice, however, really means, "it may return more than once". noreturn is interpreted as the name implies. Thus the unreachable after the call.

\-Hal


\--
Alexandre Isoard


\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
LLVM Developers mailing list  
llvm-dev@lists.llvm.org  
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev  

--   
Hal Finkel  
Lead, Compiler Technology and Programming Languages  
Leadership Computing Facility  
Argonne National Laboratory