Skip to content

Commit d7e997a

Browse files
veselypetaresistor
authored andcommitted
[clang][CodeGen] Use index type for inline asm ptrtoint
When the return type of an inline asm instruction needs to be truncated to an integer type we need to use the index type and not the size of the pointer type so as to not crash.
1 parent 3c5bfe1 commit d7e997a

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2796,7 +2796,7 @@ EmitAsmStores(CodeGenFunction &CGF, const AsmStmt &S,
27962796
Tmp = Builder.CreateIntToPtr(Tmp, TruncTy);
27972797
} else if (Tmp->getType()->isPointerTy() && TruncTy->isIntegerTy()) {
27982798
uint64_t TmpSize =
2799-
CGM.getDataLayout().getTypeSizeInBits(Tmp->getType());
2799+
CGM.getDataLayout().getIndexTypeSizeInBits(Tmp->getType());
28002800
Tmp = Builder.CreatePtrToInt(
28012801
Tmp, llvm::IntegerType::get(CTX, (unsigned)TmpSize));
28022802
Tmp = Builder.CreateTrunc(Tmp, TruncTy);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 2
2+
// RUN: %riscv32_cheri_purecap_cc1 -disable-O0-optnone %s -emit-llvm -o - | opt -passes=mem2reg -S -o - | FileCheck %s --check-prefix=RV32
3+
// RUN: %riscv64_cheri_purecap_cc1 -disable-O0-optnone %s -emit-llvm -o - | opt -passes=mem2reg -S -o - | FileCheck %s --check-prefix=RV64
4+
5+
// RV32-LABEL: define dso_local i32 @ptr_to_int
6+
// RV32-SAME: (ptr addrspace(200) noundef [[P:%.*]]) addrspace(200) #[[ATTR0:[0-9]+]] {
7+
// RV32-NEXT: entry:
8+
// RV32-NEXT: [[TMP0:%.*]] = call ptr addrspace(200) asm "", "=C,0"(ptr addrspace(200) [[P]]) #[[ATTR1:[0-9]+]], !srcloc [[META6:![0-9]+]]
9+
// RV32-NEXT: [[TMP1:%.*]] = ptrtoint ptr addrspace(200) [[TMP0]] to i32
10+
// RV32-NEXT: ret i32 [[TMP1]]
11+
//
12+
// RV64-LABEL: define dso_local signext i32 @ptr_to_int
13+
// RV64-SAME: (ptr addrspace(200) noundef [[P:%.*]]) addrspace(200) #[[ATTR0:[0-9]+]] {
14+
// RV64-NEXT: entry:
15+
// RV64-NEXT: [[TMP0:%.*]] = call ptr addrspace(200) asm "", "=C,0"(ptr addrspace(200) [[P]]) #[[ATTR1:[0-9]+]], !srcloc [[META6:![0-9]+]]
16+
// RV64-NEXT: [[TMP1:%.*]] = ptrtoint ptr addrspace(200) [[TMP0]] to i64
17+
// RV64-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32
18+
// RV64-NEXT: ret i32 [[TMP2]]
19+
//
20+
int ptr_to_int(void *p) {
21+
int i;
22+
asm ("" : "=C"(i) : "0"(p));
23+
return i;
24+
}

0 commit comments

Comments
 (0)