llvm / llvm/llvm-project

[BOLT][RISCV] TailDuplication reuses instruction labels when cloning PC-relative relocation pairs

Open
#219,913 4 comments 0 reactions 0 assignees View on GitHub
BOLT
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

## Summary

When BOLT TailDuplication clones a RISC-V basic block containing a paired
`%pcrel_hi/%pcrel_lo` address sequence, the cloned instructions retain the
original instruction label and symbol references.

During final emission, both the original and cloned `auipc` instructions define
the same `MCSymbol`, causing BOLT to fail with:

```text
:0: error: symbol '.Ltmp3' is already defined
BOLT-ERROR: Emission failed.

Minimal reproducer
Create taildup-pcrel.s:
```

```asm
.data
.p2align 3
.globl object
object:
.dword 0

.text
.p2align 1
.globl _start
.type _start, @function
_start:
beqz a0, .Lspacer
beqz a1, tail

pred:
// FDATA: 1 _start #pred# 100
j tail

.Lspacer:
ret

tail:
// FDATA: 1 _start #tail# 200
.Lpcrel_hi:
auipc t0, %pcrel_hi(object)
ld t1, %pcrel_lo(.Lpcrel_hi)(t0)
sd t1, %pcrel_lo(.Lpcrel_hi)(t0)
ret

.size _start, .-_start
```

Actual behavior
TailDuplication successfully selects and clones the tail block:
BOLT-INFO: tail duplication modified 1 (100.00%) functions;
duplicated 1 blocks (14 bytes)
However, the finalized function shows that both copies of the auipc sequence
use the same instruction label:

```asm
.Ltail-dup0:
auipc t0, %pcrel_hi(object) # Label: .Ltmp3
ld t1, %pcrel_lo(.Ltmp3)(t0)
sd t1, %pcrel_lo(.Ltmp3)(t0)
ret

.Ltmp1:
auipc t0, %pcrel_hi(object) # Label: .Ltmp3
ld t1, %pcrel_lo(.Ltmp3)(t0)
sd t1, %pcrel_lo(.Ltmp3)(t0)
ret
```

```
Final emission then fails:
:0: error: symbol '.Ltmp3' is already defined
BOLT-ERROR: Emission failed.
```

--tail-duplication=none produces an output successfully.

Contributor guide

Open the contributing guide

Research direction

Start with BOLT's TailDuplication handling and the minimal RISC-V reproducer in the issue. Check how cloned paired %pcrel_hi/%pcrel_lo instructions retain labels, then verify that the original and duplicated sequences receive distinct references and final emission succeeds instead of reporting a duplicate .Ltmp3 symbol.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.