Skip to content

[clang] Miscompilation at -O2/3 #121745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cardigan1008 opened this issue Jan 6, 2025 · 3 comments
Closed

[clang] Miscompilation at -O2/3 #121745

cardigan1008 opened this issue Jan 6, 2025 · 3 comments

Comments

@cardigan1008
Copy link

This code prints 8 at -O0/1 and prints 1 at -O2/3:

int printf(const char *, ...);
static char a;
static char *b = &a;
static int c;
short d;
void e() {
  short f[8];
  char **g[] = {&b, &b};
  c = 0;
  for (; c < 8; c = 81 + 462704684 + *b - 462704765 + c + 1)
    f[c] = 0;
  d = f[5];
}
int main() {
  e();
  printf("%d\n", c);
}

Compiler Explorer: https://godbolt.org/z/MEKz8oncP

Bisected to df4a615, which was committed by @fhahn

@cardigan1008 cardigan1008 changed the title Miscompilation at -O2/3 [clang] Miscompilation at -O2/3 Jan 6, 2025
@dtcxzyw
Copy link
Member

dtcxzyw commented Jan 6, 2025

Reduced testcase: https://alive2.llvm.org/ce/z/6Bg63G

; bin/opt -passes=loop-vectorize test.ll -S
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define i32 @src() {
entry:
  %f.i = alloca [8 x i16], align 16
  %conv.i = sext i8 0 to i32
  %invariant.op.i = add nsw i32 %conv.i, 1
  br label %for.body.i

for.body.i:                                       ; preds = %for.body.i, %entry
  %0 = phi i32 [ 0, %entry ], [ %add2.reass.i, %for.body.i ]
  %idxprom.i = sext i32 %0 to i64
  %arrayidx.i = getelementptr [8 x i16], ptr %f.i, i64 0, i64 %idxprom.i
  store i16 0, ptr %arrayidx.i, align 2
  %add2.reass.i = add i32 %invariant.op.i, %0
  %cmp.i = icmp slt i32 %add2.reass.i, 8
  br i1 %cmp.i, label %for.body.i, label %e.exit

e.exit:                                           ; preds = %for.body.i
  %add2.reass.i.lcssa = phi i32 [ %add2.reass.i, %for.body.i ]
  ret i32 %add2.reass.i.lcssa
}

@fhahn
Copy link
Contributor

fhahn commented Jan 6, 2025

Looking into it now, thanks

@yaoxinliu
Copy link

Thank God! clang19 works as expected.

fhahn added a commit that referenced this issue Jan 6, 2025
@fhahn fhahn closed this as completed in f9369cc Jan 6, 2025
github-actions bot pushed a commit to arm/arm-toolchain that referenced this issue Jan 10, 2025
github-actions bot pushed a commit to arm/arm-toolchain that referenced this issue Jan 10, 2025
…needed.

Legalize extract-from-ends using uniform VPReplicateRecipe of wide
inductions to use regular VPReplicateRecipe, so the correct end value
is available.

Fixes llvm/llvm-project#121745.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants