Skip to content

separate-const-offset-from-gep miscompile related to "trunc nuw" #154116

@mikaelholmen

Description

@mikaelholmen

llvm commit: 673750f

Reproduce with:

opt -passes=separate-const-offset-from-gep bbi-109669_sep.ll -S -o - -mtriple=hexagon

The input program does

%0 = phi i32 [ -20, %entry ]
%1 = add i32 %0, 20
%2 = trunc nuw i32 %1 to i16

where the "trunc nuw" is ok since %1 is 0. No truncated bits are set.

However, in the result we get:

%0 = phi i32 [ -20, %entry ]
[...]
%1 = trunc nuw i32 %0 to i16

so here the input to "trunc nuw" is negative and the result is poison.
Langref https://llvm.org/docs/LangRef.html#trunc-to-instruction says
"If the nuw keyword is present, and any of the truncated bits are non-zero, the result is a poison value."

Then %1 is further used to compute the return value from the function so the whole thing results in poison which it did not in the input.

(I stumbled upon this as a miscompile after
#152990
which I first incorrectly blamed.)

bbi-109669_sep.ll.gz

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions