# Always extend the integer parameters of callee

**URL:** <https://discourse.llvm.org/t/always-extend-the-integer-parameters-of-callee/61319>\
**Category:** X86\
**Tags:** x86, clang\
**Created:** [March 30, 2022, 6:30am UTC](https://discourse.llvm.org/t/always-extend-the-integer-parameters-of-callee/61319 "2022-03-30T06:30:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Liu\_Chen3](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/liu_chen3/32/14045_2.png) [@Liu\_Chen3](https://discourse.llvm.org/u/Liu_Chen3)\
**Post date:** [March 30, 2022, 6:30am UTC](https://discourse.llvm.org/t/always-extend-the-integer-parameters-of-callee/61319/1 "2022-03-30T06:30:34Z")

</div>

Hi, all  
Currently we found that when clang handles 8/16 bits integer parameter, it will always assume the parameter has been sign/zero extend. This will cause some ABI compatibility issues: [Compiler Explorer](https://gcc.godbolt.org/z/8aaPP33cG) .

The summary is as follows:

clang: caller do the zero/sign extension while callee do nothing.  
gcc: both caller and callee do the zero/sign extension.  
icc: callee do the zero/sign extension while caller do nothong.

Now we want to make a patch that make clang follow the gcc behavior by removing the zeroext/signext attribute from the parameter: [Compiler Explorer](https://godbolt.org/z/Mxh4zasch) .  
Any comments here?

Thanks.

---

<div class="post-metadata">

**Author:** ![Liu\_Chen3](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/liu_chen3/32/14045_2.png) [@Liu\_Chen3](https://discourse.llvm.org/u/Liu_Chen3)\
**Post date:** [March 30, 2022, 6:54am UTC](https://discourse.llvm.org/t/always-extend-the-integer-parameters-of-callee/61319/2 "2022-03-30T06:54:38Z")

</div>

Hi, @rjmccall , @topperc , what’s your opinion here?

---

<div class="post-metadata">

**Author:** ![topperc](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/topperc/32/1293_2.png) [@topperc](https://discourse.llvm.org/u/topperc)\
**Post date:** [March 30, 2022, 7:04am UTC](https://discourse.llvm.org/t/always-extend-the-integer-parameters-of-callee/61319/3 "2022-03-30T07:04:15Z")

</div>

For completeness, there were previous discussions on this here [⚙ D72742 Don't assume promotable integers are zero/sign-extended already in x86-64 ABI.](https://reviews.llvm.org/D72742) and here [44228 – clang assumes zero-extension of 8-bit arguments in x86, causing interop issues with gcc](https://bugs.llvm.org/show_bug.cgi?id=44228)

---

<div class="post-metadata">

**Author:** ![jyknight](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/jyknight/32/1253_2.png) [@jyknight](https://discourse.llvm.org/u/jyknight)\
**Post date:** [March 30, 2022, 2:41pm UTC](https://discourse.llvm.org/t/always-extend-the-integer-parameters-of-callee/61319/4 "2022-03-30T14:41:40Z")

</div>

Migrated bug link: [https://github.com/llvm/llvm-project/issues/43573](https://github.com/llvm/llvm-project/issues/43573)

---

<div class="post-metadata">

**Author:** ![topperc](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/topperc/32/1293_2.png) [@topperc](https://discourse.llvm.org/u/topperc)\
**Post date:** [March 30, 2022, 4:03pm UTC](https://discourse.llvm.org/t/always-extend-the-integer-parameters-of-callee/61319/5 "2022-03-30T16:03:25Z")

</div>

If we remove the attribute, does that meant that new versions of clang won’t be able to call code compiled with old versions of clang because the caller(new clang) won’t extend the operands like the callee(old clang) code expects?

---

<div class="post-metadata">

**Author:** ![Liu\_Chen3](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/liu_chen3/32/14045_2.png) [@Liu\_Chen3](https://discourse.llvm.org/u/Liu_Chen3)\
**Post date:** [March 31, 2022, 2:48am UTC](https://discourse.llvm.org/t/always-extend-the-integer-parameters-of-callee/61319/6 "2022-03-31T02:48:40Z")

</div>

This is not what I expected. We want do the extension on both caller and callee. It looks like we can’t simply remove this attribute in the FE. I checked the `CodeGenModule::ConstructAttributeList` in CGCall.cpp and didn’t find anything we can do about this attribute.  
At present the way I can think of is to ignore sign/zero attribute in the X86 backend.

---

<div class="post-metadata">

**Author:** ![Liu\_Chen3](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/liu_chen3/32/14045_2.png) [@Liu\_Chen3](https://discourse.llvm.org/u/Liu_Chen3)\
**Post date:** [April 2, 2022, 5:59am UTC](https://discourse.llvm.org/t/always-extend-the-integer-parameters-of-callee/61319/7 "2022-04-02T05:59:08Z")

</div>

One candidate patch : [⚙ D122963 [X86] Extend the integer parameter if the function isn't local linked](https://reviews.llvm.org/D122963)

---

<div class="post-metadata">

**Author:** ![emilio](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/emilio/32/14073_2.png) [@emilio](https://discourse.llvm.org/u/emilio)\
**Post date:** [April 2, 2022, 11:44am UTC](https://discourse.llvm.org/t/always-extend-the-integer-parameters-of-callee/61319/8 "2022-04-02T11:44:31Z")

</div>

FWIW I reworked that patch to actually remove the zeroext/signext attributes in [⚙ D72742 Don't assume promotable integers are zero/sign-extended already in x86-64 ABI.](https://reviews.llvm.org/D72742). But maybe the code has changed a lot since then and it can’t be done that way?

---

<div class="post-metadata">

**Author:** ![JohnReagan](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@JohnReagan](https://discourse.llvm.org/u/JohnReagan)\
**Post date:** [April 4, 2022, 5:53pm UTC](https://discourse.llvm.org/t/always-extend-the-integer-parameters-of-callee/61319/9 "2022-04-04T17:53:25Z")

</div>

On our legacy systems, we provide an “assume clean parameters” option to tell the compiler that the caller sign/zero extended out to the width of the argument slot. If not, the called routine will have to do the extension as needed. We default to “assume clean parameters” on Alpha and Itanium, but should change that to “don’t assume clean parameters” on x86.

On OpenVMS, our ABI on Alpha and Itanium specifies that the caller extend the argument and so far we’ve been riding along on LLVM doing something similar but we’ll have to do OpenVMS-specific changes. For example, we sign-extend 32-bit unsigned parameters (yes, sign extend, not zero extend). It helps legacy programs with mismatched prototypes when then moved from the 32-bit VAX to later 64-bit systems like Alpha and Itanium.

---

<div class="post-metadata">

**Author:** ![Liu\_Chen3](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/liu_chen3/32/14045_2.png) [@Liu\_Chen3](https://discourse.llvm.org/u/Liu_Chen3)\
**Post date:** [April 6, 2022, 2:24am UTC](https://discourse.llvm.org/t/always-extend-the-integer-parameters-of-callee/61319/10 "2022-04-06T02:24:50Z")

</div>

If we remove the attribute on the FE, it will cause the caller to no longer do zero/sign extension. Just as Craig said, this will cause compatibility issues with previous clang.
