[llvm] [BPF] Report warning for some insn imm requiring valid range in inline asm (PR #142989)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 12 18:55:53 PDT 2025
https://github.com/yonghong-song updated https://github.com/llvm/llvm-project/pull/142989
>From 41107109822a47f18ea5330c111cab09adbf486b Mon Sep 17 00:00:00 2001
From: Yonghong Song <yonghong.song at linux.dev>
Date: Wed, 4 Jun 2025 16:22:53 -0700
Subject: [PATCH] [BPF] Report an error if comparison imm operand cannot fit in
32bit
Ihor Solodrai reported a case ([1]) where gcc reports an error but clang
ignores that error and proceeds to generate incorrect code. More
specifically, the problematic code looks like:
if r1 == 0xcafefeeddeadbeef goto <label>
Here, 0xcafefeeddeadbeef needs to be encoded in a 32-bit imm field
of the insns and the 32-bit imm allows sign extenstion to 64-bit imm.
Obviously, 0xcafefeeddeadbeef cannot encode properly.
The compilation failed for gcc with the following error:
Error: immediate out of range, shall fit in 32 bits
Given a 64-bit imm value, converting to the proper 32-bit imm value
must satisfy the following 64-bit patterns:
00000000 00000000 00000000 00000000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
11111111 11111111 11111111 11111111 1xxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
So if the top 32-bits is 0 or the top 33-bits is 0x1ffffffff, then the 64-bit imm
value can be truncated into proper 32-bit imm. Otherwise, a warning
message, the same as gcc will be issued. If -Werror is enabled during
compilation, the warning will turn into an error.
[1] https://lore.kernel.org/bpf/f93ce37e-e155-4165-88e2-1a3cadee7c82@linux.dev/
---
.../BPF/MCTargetDesc/BPFMCCodeEmitter.cpp | 22 ++++++++++++++-----
llvm/test/CodeGen/BPF/warn-cmp.ll | 15 +++++++++++++
2 files changed, 31 insertions(+), 6 deletions(-)
create mode 100644 llvm/test/CodeGen/BPF/warn-cmp.ll
diff --git a/llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp b/llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
index 10a46f100bbea..bd9d2de58c8b2 100644
--- a/llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
+++ b/llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
@@ -14,6 +14,7 @@
#include "MCTargetDesc/BPFMCTargetDesc.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/MC/MCCodeEmitter.h"
+#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCFixup.h"
#include "llvm/MC/MCInst.h"
@@ -33,11 +34,12 @@ namespace {
class BPFMCCodeEmitter : public MCCodeEmitter {
const MCRegisterInfo &MRI;
bool IsLittleEndian;
+ MCContext &Ctx;
public:
BPFMCCodeEmitter(const MCInstrInfo &, const MCRegisterInfo &mri,
- bool IsLittleEndian)
- : MRI(mri), IsLittleEndian(IsLittleEndian) { }
+ bool IsLittleEndian, MCContext &ctx)
+ : MRI(mri), IsLittleEndian(IsLittleEndian), Ctx(ctx) {}
BPFMCCodeEmitter(const BPFMCCodeEmitter &) = delete;
void operator=(const BPFMCCodeEmitter &) = delete;
~BPFMCCodeEmitter() override = default;
@@ -67,12 +69,12 @@ class BPFMCCodeEmitter : public MCCodeEmitter {
MCCodeEmitter *llvm::createBPFMCCodeEmitter(const MCInstrInfo &MCII,
MCContext &Ctx) {
- return new BPFMCCodeEmitter(MCII, *Ctx.getRegisterInfo(), true);
+ return new BPFMCCodeEmitter(MCII, *Ctx.getRegisterInfo(), true, Ctx);
}
MCCodeEmitter *llvm::createBPFbeMCCodeEmitter(const MCInstrInfo &MCII,
MCContext &Ctx) {
- return new BPFMCCodeEmitter(MCII, *Ctx.getRegisterInfo(), false);
+ return new BPFMCCodeEmitter(MCII, *Ctx.getRegisterInfo(), false, Ctx);
}
unsigned BPFMCCodeEmitter::getMachineOpValue(const MCInst &MI,
@@ -81,8 +83,16 @@ unsigned BPFMCCodeEmitter::getMachineOpValue(const MCInst &MI,
const MCSubtargetInfo &STI) const {
if (MO.isReg())
return MRI.getEncodingValue(MO.getReg());
- if (MO.isImm())
- return static_cast<unsigned>(MO.getImm());
+ if (MO.isImm()) {
+ uint64_t Imm = MO.getImm();
+ uint64_t High32Bits = Imm >> 32, High33Bits = Imm >> 31;
+ if (MI.getOpcode() != BPF::LD_imm64 && High32Bits != 0 &&
+ High33Bits != 0x1FFFFFFFFULL) {
+ Ctx.reportWarning(MI.getLoc(),
+ "immediate out of range, shall fit in 32 bits");
+ }
+ return static_cast<unsigned>(Imm);
+ }
assert(MO.isExpr());
diff --git a/llvm/test/CodeGen/BPF/warn-cmp.ll b/llvm/test/CodeGen/BPF/warn-cmp.ll
new file mode 100644
index 0000000000000..109d177b0fb42
--- /dev/null
+++ b/llvm/test/CodeGen/BPF/warn-cmp.ll
@@ -0,0 +1,15 @@
+; RUN: llc -mtriple=bpfel -filetype=obj < %s 2>&1 >/dev/null | FileCheck %s
+
+; CHECK: warning: immediate out of range, shall fit in 32 bits
+define dso_local void @test_1() naked {
+ tail call void asm sideeffect
+ "r1 = 40; if r1 == 0x1deadbeef goto +0; r0 = 0; exit;", "~{r0},~{r1}"()
+ unreachable
+}
+
+; CHECK: warning: immediate out of range, shall fit in 32 bits
+define dso_local void @test_2() naked {
+ tail call void asm sideeffect
+ "r1 = 40; if r1 == 0xffffffff00000000 goto +0; r0 = 0; exit;", "~{r0},~{r1}"()
+ unreachable
+}
More information about the llvm-commits
mailing list