Issue 242341
Summary [LoongArch] Wrong addenda for 2GiB+ offset access in "large" code model
Labels new issue
Assignees
Reporter CSharperMantle
    On LoongArch64, the following programs ...

```llvm
; obj.ll

@g_a64 = dso_local global [2147483649 x i8] zeroinitializer, align 8

define dso_local ptr @get_last() nounwind {
entry:
  ret ptr getelementptr inbounds ([2147483649 x i8], ptr @g_a64, i64 0, i64 2147483648)
}
```

... with the following driver ...

```c
// main.c

#include <stdio.h>
#include <stdint.h>

extern char g_a64[];
extern char *get_last(void);

int main(void) {
    char *p = get_last();
    uintptr_t d = (uintptr_t)p - (uintptr_t)g_a64;
 fprintf(stderr, "delta = %llu (%p), expected 2147483648\n", (unsigned long long)d, d);
    *p = 42;
    fprintf(stderr, "wrote 42, read %d\n", (int)*p);
    return 0;
}
```

... miscompiles with the "large" code model with LLVM 23.1.4:

```console
$ llc --version | head -n 6
LLVM (http://llvm.org/):
  LLVM version 23.1.4
  Optimized build.
  Default target: loongarch64-unknown-linux-gnu
  Host CPU: la664

$ llc --mtriple=loongarch64 -filetype=obj -code-model=large -o obj.o obj.ll
$ gcc -o test main.c obj.o
$ ./test
delta = 18442240476229664768 (0xfff0000080000000), expected 2147483648
Bus error                  (core dumped) ./test
$ objdump --reloc --disassemble ./obj.o

./obj.o:     file format elf64-loongarch


Disassembly of section .text:

0000000000000000 <get_last>:
   0:   1a000004        pcalau12i       $a0, 0
 0: R_LARCH_PCALA_HI20   g_a64-0xfffff80000000
   4:   02c00005 li.d            $a1, 0
                        4: R_LARCH_PCALA_LO12 g_a64-0xfffff80000000
   8:   16000005        lu32i.d         $a1, 0
 8: R_LARCH_PCALA64_LO20 g_a64-0xfffff80000000
   c: 030000a5        lu52i.d         $a1, $a1, 0
                        c: R_LARCH_PCALA64_HI12 g_a64-0xfffff80000000
  10:   001090a4        add.d $a0, $a1, $a0
  14:   4c000020        ret
```

It seems that the addenda are miscalculated.

I'm working on a fix locally. It blocks an optimization I'm planning to work on.

CC @SixWeining, @wangleiat
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to