[PATCH] D84220: [IPSCCP] Fix a bug that the "returned" attribute is not cleared when function is optimized to return undef

Congzhe Cao via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 30 13:00:48 PDT 2020


congzhe added a comment.

In D84220#2179859 <https://reviews.llvm.org/D84220#2179859>, @efriedma wrote:

>> I'm not sure I understand the motivation. My problem is that if we replace the return values with undef, it should mean there is no use of the returned value of the function live anymore. What am I missing? Maybe provide the motivating example as a test case?
>
> We use "returned" in codegen even if the value has no uses; see https://reviews.llvm.org/D64986 .

Yes that might be a concern (IMHO). Even if the value has no uses, there might be other values aliasing with it, causing “uses” of the return value.

Just adding more background information about the bug. It is exposed in the following piece of pseudocode (a simplified version of the whole testing code, which is a bit complicated):

  function_x()
  
  function_y()
  
  int* a, b  // a and b are detected to be alias 
  
  int main {
        a = function_y();
        function_x(b);
  }

In IPSCCP the LLVM determines to zap function_y() to return undef, and since the first argument of `function_y()` has the `returned` attribute, during register allocation the regmask for `function_y` indicates `w0` is preserved.

In AArch64 assembly after `bl y` instruction, it keeps using `w0` as the return value of `function_y()` (i.e., `a`).  `w0` will continue to be passed to `function_x()` as the value of `b` . However, inside `function_y()` it does not `mov` the return value to `w0` and `w0` is just clobbered and therefore contains dirty values. Hence what gets passed to `function_x()` is incorrect, making the rest of program execute incorrectly.

Clearing the `returned` attribute in this patch fixes this issue. With the patch `w0` now contains the correct value during the call of `function_y()`.

Hope it helps -


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D84220/new/

https://reviews.llvm.org/D84220



More information about the llvm-commits mailing list