<div dir="ltr"><div>This may be a silly question, but... why aren't all ABI-relevant facts about functions encoded in the function type? If you zoom back out from the problem sufficiently far, it seems that today, some ABI-relevant facts are part of the pointer type (argument & return types) while others aren't. It seems to me that a saner design would have everything in the same place. Which means one of:</div><div><br></div><div>1. All ABI-relevant information, including calling convention and byval, is part of the function type.</div><div><br></div><div>2. Function types become "opaque" (with opaque pointers, perhaps FunctionType is eliminated entirely?) and all relevant information is stored with the call instruction, independent of direct vs. indirect calls.<br></div><div><br></div><div>Cheers,</div><div>Nicolai<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jun 23, 2021 at 10:01 PM Reid Kleckner via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">I think the trouble we are having comes from the fact that we don't make it easy for IR producers (frontends or instrumentation passes) to do the right thing. If we make it easy (almost automatic), then maybe it will be more reasonable to declare in LangRef that function prototype mismatch is in fact UB.<div><br></div><div>Another developer suggested that the IRBuilder should take responsibility for setting the attributes on the call site, but I don't like the idea of IRBuilder::CreateCall inspecting the Callee argument with dyn_cast to decide its behavior.<div><br></div><div>This might be an infeasibly difficult ABI breaking change, but what if we had two overloads, one for direct calls, and one for indirect calls? Maybe we already have that in the current set of overloads:</div><div>  CallInst *CreateCall(FunctionType *FTy, Value *Callee,<br>                       ArrayRef<Value *> Args = None, const Twine &Name = "",<br>                       MDNode *FPMathTag = nullptr);<br>  CallInst *CreateCall(FunctionType *FTy, Value *Callee, ArrayRef<Value *> Args,<br>                       ArrayRef<OperandBundleDef> OpBundles,<br>                       const Twine &Name = "", MDNode *FPMathTag = nullptr);<br>  CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args = None,<br>                       const Twine &Name = "", MDNode *FPMathTag = nullptr);</div><div>  CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args,<br>                       ArrayRef<OperandBundleDef> OpBundles,<br>                       const Twine &Name = "", MDNode *FPMathTag = nullptr);</div><div><br></div><div>Assuming the vast majority of callers use the FunctionCallee overload, which is implicitly constructible from a Function*, we could extend FunctionCallee to carry the calling convention and an attribute list. The CC and the attributes are actually part of clang::FunctionType, they just aren't part of llvm::FunctionType. FunctionCallee is sort of incomplete without them.</div><div><br></div><div>So, the task would be to add non-optional parameters to the FunctionType/Value overload (used for indirect calls) and then fix up all the call sites. Frontends that generate indirect calls would observe this as build breakage, not changes to generated code. Instrumentation passes would magically start doing the right thing without any source changes. This would also address the long-standing usability issue of calling conventions [1], which are optimized to unreachable if you forget to set the calling convention on the call site.</div></div><div><br></div><div>[1] <a href="https://llvm.org/docs/FAQ.html#why-does-instcombine-simplifycfg-turn-a-call-to-a-function-with-a-mismatched-calling-convention-into-unreachable-why-not-make-the-verifier-reject-it" target="_blank">https://llvm.org/docs/FAQ.html#why-does-instcombine-simplifycfg-turn-a-call-to-a-function-with-a-mismatched-calling-convention-into-unreachable-why-not-make-the-verifier-reject-it</a></div><div><br></div><div>---</div><div><br></div><div>Having written this up, this seems like a good direction. Credit goes to the person that originally suggested the IRBuilder approach, I just had to think harder about it. :)</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jun 22, 2021 at 6:06 PM Arthur Eubanks via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Currently ABI attributes are weirdly handled if an argument to a function is missing an ABI attribute that is present on the called function's corresponding parameter. e.g.</div><div><br></div><div><font face="monospace">declare void <a class="gmail_plusreply" id="gmail-m_-6188152385369565523gmail-m_-4081632343403966207plusReplyChip-0">@f(i32* byval(i32))</a></font></div><div><a class="gmail_plusreply">d</a><font face="monospace">efine void @g() {</font></div><div><font face="monospace"> %a = alloca i32</font></div><div><font face="monospace"> call void @f(i32* %a) ; missing the byval(i32) attribute</font></div><div><font face="monospace"> ret void</font><a class="gmail_plusreply"><br></a></div><div><font face="monospace">}</font></div><div><br></div>CallBase::isByValArgument(unsigned ArgNo) forwards to CallBase::paramHasAttr(), which first checks the argument attributes, then if the call is a direct call, checks the called function's parameter attributes. The existing implementation of CallBase::paramHasAttr() makes sense for optimization attributes like nocapture, but doesn't really make sense for ABI attributes like byval. It's weird that lowering a call may be different depending on whether or not the call is direct.<div><div><br></div><div>I attempted to only have lowering inspect the ABI attributes on the argument and not look through at a potentially direct callee, but there were cases where LLVM was generating direct calls to functions with ABI attributes but without properly setting the ABI attributes on the arguments. I fixed a couple of these but ended up reverting everything since it was still unclear if we wanted to go in this direction.</div><div><br></div><div>Should we go down the path of ignoring ABI attributes on direct callees and only looking at the attributes on the arguments? And in that case we may generate code that crashes at runtime if ABI attributes don't properly match. Otherwise we should document the existing behavior in the LangRef. The LangRef only mandates that <a href="https://llvm.org/docs/LangRef.html#id327" target="_blank">ABI attributes match on a musttail call</a>.</div><div></div></div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature">Lerne, wie die Welt wirklich ist,<br>aber vergiss niemals, wie sie sein sollte.</div>