[llvm] 62b1682 - [Opaqueptrs][IR Serialization] Improve inlineasm [de]serialization
Roman Lebedev via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 30 02:59:14 PST 2021
Author: Roman Lebedev
Date: 2021-12-30T13:54:37+03:00
New Revision: 62b1682570b1059e8c6542192159dcde32c13c30
URL: https://github.com/llvm/llvm-project/commit/62b1682570b1059e8c6542192159dcde32c13c30
DIFF: https://github.com/llvm/llvm-project/commit/62b1682570b1059e8c6542192159dcde32c13c30.diff
LOG: [Opaqueptrs][IR Serialization] Improve inlineasm [de]serialization
The bitcode reader expected that the pointers are typed,
so that it can extract the function type for the assembly
so `bitc::CST_CODE_INLINEASM` did not explicitly store said function type.
I'm not really sure how the upgrade path will look for existing bitcode,
but i think we can easily support opaque pointers going forward,
by simply storing the function type.
Reviewed By: #opaque-pointers, nikic
Differential Revision: https://reviews.llvm.org/D116341
Added:
Modified:
llvm/include/llvm/Bitcode/LLVMBitCodes.h
llvm/lib/Bitcode/Reader/BitcodeReader.cpp
llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
llvm/test/Bitcode/callbr.ll
Removed:
################################################################################
diff --git a/llvm/include/llvm/Bitcode/LLVMBitCodes.h b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
index 7301618d337a0..6d0f51ce9c6d0 100644
--- a/llvm/include/llvm/Bitcode/LLVMBitCodes.h
+++ b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
@@ -381,10 +381,14 @@ enum ConstantsCodes {
CST_CODE_CE_UNOP = 25, // CE_UNOP: [opcode, opval]
CST_CODE_POISON = 26, // POISON
CST_CODE_DSO_LOCAL_EQUIVALENT = 27, // DSO_LOCAL_EQUIVALENT [gvty, gv]
- CST_CODE_INLINEASM = 28, // INLINEASM: [sideeffect|alignstack|
+ CST_CODE_INLINEASM_OLD3 = 28, // INLINEASM: [sideeffect|alignstack|
+ // asmdialect|unwind,
+ // asmstr,conststr]
+ CST_CODE_NO_CFI_VALUE = 29, // NO_CFI [ fty, f ]
+ CST_CODE_INLINEASM = 30, // INLINEASM: [fnty,
+ // sideeffect|alignstack|
// asmdialect|unwind,
// asmstr,conststr]
- CST_CODE_NO_CFI_VALUE = 29, // NO_CFI [ fty, f ]
};
/// CastOpcodes - These are values used in the bitcode files to encode which
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 3360ad1c23500..93b3dbf525f1f 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -2824,6 +2824,7 @@ Error BitcodeReader::parseConstants() {
for (unsigned i = 0; i != ConstStrSize; ++i)
ConstrStr += (char)Record[3+AsmStrSize+i];
UpgradeInlineAsmString(&AsmStr);
+ // FIXME: support upgrading in opaque pointers mode.
V = InlineAsm::get(
cast<FunctionType>(cast<PointerType>(CurTy)->getElementType()),
AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
@@ -2850,6 +2851,7 @@ Error BitcodeReader::parseConstants() {
for (unsigned i = 0; i != ConstStrSize; ++i)
ConstrStr += (char)Record[3+AsmStrSize+i];
UpgradeInlineAsmString(&AsmStr);
+ // FIXME: support upgrading in opaque pointers mode.
V = InlineAsm::get(
cast<FunctionType>(cast<PointerType>(CurTy)->getElementType()),
AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
@@ -2857,7 +2859,7 @@ Error BitcodeReader::parseConstants() {
break;
}
// This version adds support for the unwind keyword.
- case bitc::CST_CODE_INLINEASM: {
+ case bitc::CST_CODE_INLINEASM_OLD3: {
if (Record.size() < 2)
return error("Invalid record");
unsigned OpNum = 0;
@@ -2881,12 +2883,46 @@ Error BitcodeReader::parseConstants() {
for (unsigned i = 0; i != ConstStrSize; ++i)
ConstrStr += (char)Record[OpNum + AsmStrSize + i];
UpgradeInlineAsmString(&AsmStr);
+ // FIXME: support upgrading in opaque pointers mode.
V = InlineAsm::get(
cast<FunctionType>(cast<PointerType>(CurTy)->getElementType()),
AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
InlineAsm::AsmDialect(AsmDialect), CanThrow);
break;
}
+ // This version adds explicit function type.
+ case bitc::CST_CODE_INLINEASM: {
+ if (Record.size() < 3)
+ return error("Invalid record");
+ unsigned OpNum = 0;
+ auto *FnTy = dyn_cast_or_null<FunctionType>(getTypeByID(Record[OpNum]));
+ ++OpNum;
+ if (!FnTy)
+ return error("Invalid record");
+ std::string AsmStr, ConstrStr;
+ bool HasSideEffects = Record[OpNum] & 1;
+ bool IsAlignStack = (Record[OpNum] >> 1) & 1;
+ unsigned AsmDialect = (Record[OpNum] >> 2) & 1;
+ bool CanThrow = (Record[OpNum] >> 3) & 1;
+ ++OpNum;
+ unsigned AsmStrSize = Record[OpNum];
+ ++OpNum;
+ if (OpNum + AsmStrSize >= Record.size())
+ return error("Invalid record");
+ unsigned ConstStrSize = Record[OpNum + AsmStrSize];
+ if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size())
+ return error("Invalid record");
+
+ for (unsigned i = 0; i != AsmStrSize; ++i)
+ AsmStr += (char)Record[OpNum + i];
+ ++OpNum;
+ for (unsigned i = 0; i != ConstStrSize; ++i)
+ ConstrStr += (char)Record[OpNum + AsmStrSize + i];
+ UpgradeInlineAsmString(&AsmStr);
+ V = InlineAsm::get(FnTy, AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
+ InlineAsm::AsmDialect(AsmDialect), CanThrow);
+ break;
+ }
case bitc::CST_CODE_BLOCKADDRESS:{
if (Record.size() < 3)
return error("Invalid record");
@@ -4783,7 +4819,7 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
cast<PointerType>(Callee->getType())->getElementType());
if (!FTy)
return error("Callee is not of pointer to function type");
- } else if (cast<PointerType>(Callee->getType())->getElementType() != FTy)
+ } else if (!OpTy->isOpaqueOrPointeeTypeMatches(FTy))
return error("Explicit call type does not match pointee type of "
"callee operand");
if (Record.size() < FTy->getNumParams() + OpNum)
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index dc06bc10cf952..e0efdf286caff 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -2458,6 +2458,7 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
}
if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
+ Record.push_back(VE.getTypeID(IA->getFunctionType()));
Record.push_back(
unsigned(IA->hasSideEffects()) | unsigned(IA->isAlignStack()) << 1 |
unsigned(IA->getDialect() & 1) << 2 | unsigned(IA->canThrow()) << 3);
diff --git a/llvm/test/Bitcode/callbr.ll b/llvm/test/Bitcode/callbr.ll
index ecc397ac75467..648d2f11b0d45 100644
--- a/llvm/test/Bitcode/callbr.ll
+++ b/llvm/test/Bitcode/callbr.ll
@@ -1,10 +1,13 @@
-; RUN: llvm-dis < %s.bc | FileCheck %s
-
+; RUN: llvm-dis < %s.bc | FileCheck %s --check-prefixes=CHECK,CHECK-TYPED
; callbr.ll.bc was generated by passing this file to llvm-as.
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s --check-prefixes=CHECK,CHECK-TYPED
+; RUN: llvm-as -opaque-pointers < %s | llvm-dis -opaque-pointers | FileCheck %s --check-prefixes=CHECK,CHECK-OPAQUE
+
define i32 @test_asm_goto(i32 %x){
entry:
-; CHECK: callbr void asm "", "r,X"(i32 %x, i8* blockaddress(@test_asm_goto, %fail))
+; CHECK-TYPED: callbr void asm "", "r,X"(i32 %x, i8* blockaddress(@test_asm_goto, %fail))
+; CHECK-OPAQUE: callbr void asm "", "r,X"(i32 %x, ptr blockaddress(@test_asm_goto, %fail))
; CHECK-NEXT: to label %normal [label %fail]
callbr void asm "", "r,X"(i32 %x, i8* blockaddress(@test_asm_goto, %fail)) to label %normal [label %fail]
normal:
More information about the llvm-commits
mailing list