Skip to content

Commit 5686786

Browse files
yonghong-songYonghong Song
and
Yonghong Song
authored
[BPF] Fix BitCast Assertion with NonZero AddrSpace (#130722)
Alexei reported a bpf selftest failure with recent llvm for bpf prog file progs/arena_spin_lock.c. The failure only happens when clang is built with cmake option LLVM_ENABLE_ASSERTIONS=ON. The error message looks like: ``` clang: /home/yhs/work/yhs/llvm-project/llvm/lib/IR/Instructions.cpp:3460: llvm::BitCastInst::BitCastInst(Value *, Type *, const Twine &, InsertPosition): Assertion `castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"' failed. ``` Further investigation shows that the problem is triggered in BPF/BPFAbstractMemberAccess.cpp for code ``` auto *BCInst = new BitCastInst(Base, PointerType::getUnqual(BB->getContext())); ``` For the above BitCastInst, Since 'Base' has non-zero AddrSapce, the compiler expects the type also has the same AddrSpace. But the above PointerType::getUnqual(...) does not have AddrSpace and hence causes the assertion failure. Providing the proper AddrSpace for the BitCast type fixed the issue. Co-authored-by: Yonghong Song <[email protected]>
1 parent 313e2ef commit 5686786

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,8 +1113,9 @@ bool BPFAbstractMemberAccess::transformGEPChain(CallInst *Call,
11131113
Call->getIterator());
11141114

11151115
// Generate a BitCast
1116-
auto *BCInst =
1117-
new BitCastInst(Base, PointerType::getUnqual(BB->getContext()));
1116+
auto *BCInst = new BitCastInst(
1117+
Base, PointerType::get(BB->getContext(),
1118+
Base->getType()->getPointerAddressSpace()));
11181119
BCInst->insertBefore(Call->getIterator());
11191120

11201121
// Generate a GetElementPtr
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
; RUN: opt -O2 %s | llvm-dis > %t1
2+
; RUN: llc -mcpu=v3 -filetype=asm -o - %t1 | FileCheck %s
3+
; Source code:
4+
; struct lock_t {
5+
; int counter;
6+
; } __attribute__((preserve_access_index));
7+
;
8+
; #define __arena __attribute__((address_space(1)))
9+
; int test(struct lock_t __arena *lock, unsigned val)
10+
; {
11+
; return __sync_val_compare_and_swap((&lock->counter), val, 1);
12+
; }
13+
; Compilation flag:
14+
; clang -target bpf -O2 -g -S -emit-llvm -Xclang -disable-llvm-passes arena_bitcast.c
15+
16+
target triple = "bpf"
17+
18+
%struct.lock_t = type { i32 }
19+
20+
; Function Attrs: nounwind
21+
define dso_local i32 @test(ptr addrspace(1) noundef %lock, i32 noundef %val) #0 !dbg !7 {
22+
entry:
23+
%lock.addr = alloca ptr addrspace(1), align 8
24+
%val.addr = alloca i32, align 4
25+
store ptr addrspace(1) %lock, ptr %lock.addr, align 8, !tbaa !19
26+
#dbg_declare(ptr %lock.addr, !17, !DIExpression(), !24)
27+
store i32 %val, ptr %val.addr, align 4, !tbaa !25
28+
#dbg_declare(ptr %val.addr, !18, !DIExpression(), !27)
29+
%0 = load ptr addrspace(1), ptr %lock.addr, align 8, !dbg !28, !tbaa !19
30+
%1 = call ptr addrspace(1) @llvm.preserve.struct.access.index.p1.p1(ptr addrspace(1) elementtype(%struct.lock_t) %0, i32 0, i32 0), !dbg !29, !llvm.preserve.access.index !12
31+
%2 = load i32, ptr %val.addr, align 4, !dbg !30, !tbaa !25
32+
%3 = cmpxchg ptr addrspace(1) %1, i32 %2, i32 1 seq_cst seq_cst, align 4, !dbg !31
33+
%4 = extractvalue { i32, i1 } %3, 0, !dbg !31
34+
ret i32 %4, !dbg !32
35+
}
36+
; CHECK: r1 = addr_space_cast(r1, 0, 1)
37+
38+
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
39+
declare ptr addrspace(1) @llvm.preserve.struct.access.index.p1.p1(ptr addrspace(1), i32 immarg, i32 immarg) #1
40+
41+
attributes #0 = { nounwind "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
42+
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(none) }
43+
44+
!llvm.dbg.cu = !{!0}
45+
!llvm.module.flags = !{!2, !3, !4, !5}
46+
!llvm.ident = !{!6}
47+
48+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 21.0.0git (https://github.com/llvm/llvm-project.git 7d4d8509cbec7eecd8aaf2510015b54bc5c173e1)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
49+
!1 = !DIFile(filename: "arena_bitcast.c", directory: "/root/home/yhs/tests/arena/simple", checksumkind: CSK_MD5, checksum: "51cb51c1fc09d3033dbd9aee9044dc9b")
50+
!2 = !{i32 7, !"Dwarf Version", i32 5}
51+
!3 = !{i32 2, !"Debug Info Version", i32 3}
52+
!4 = !{i32 1, !"wchar_size", i32 4}
53+
!5 = !{i32 7, !"frame-pointer", i32 2}
54+
!6 = !{!"clang version 21.0.0git (https://github.com/llvm/llvm-project.git 7d4d8509cbec7eecd8aaf2510015b54bc5c173e1)"}
55+
!7 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 6, type: !8, scopeLine: 7, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !16)
56+
!8 = !DISubroutineType(types: !9)
57+
!9 = !{!10, !11, !15}
58+
!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
59+
!11 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 64)
60+
!12 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "lock_t", file: !1, line: 1, size: 32, elements: !13)
61+
!13 = !{!14}
62+
!14 = !DIDerivedType(tag: DW_TAG_member, name: "counter", scope: !12, file: !1, line: 2, baseType: !10, size: 32)
63+
!15 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
64+
!16 = !{!17, !18}
65+
!17 = !DILocalVariable(name: "lock", arg: 1, scope: !7, file: !1, line: 6, type: !11)
66+
!18 = !DILocalVariable(name: "val", arg: 2, scope: !7, file: !1, line: 6, type: !15)
67+
!19 = !{!20, !20, i64 0}
68+
!20 = !{!"p1 _ZTS6lock_t", !21, i64 0}
69+
!21 = !{!"any pointer", !22, i64 0}
70+
!22 = !{!"omnipotent char", !23, i64 0}
71+
!23 = !{!"Simple C/C++ TBAA"}
72+
!24 = !DILocation(line: 6, column: 33, scope: !7)
73+
!25 = !{!26, !26, i64 0}
74+
!26 = !{!"int", !22, i64 0}
75+
!27 = !DILocation(line: 6, column: 48, scope: !7)
76+
!28 = !DILocation(line: 8, column: 40, scope: !7)
77+
!29 = !DILocation(line: 8, column: 46, scope: !7)
78+
!30 = !DILocation(line: 8, column: 56, scope: !7)
79+
!31 = !DILocation(line: 8, column: 10, scope: !7)
80+
!32 = !DILocation(line: 8, column: 3, scope: !7)

0 commit comments

Comments
 (0)