[llvm] 28b9771 - [OpaquePtr] Make GEPs work with opaque pointers
Arthur Eubanks via llvm-commits
llvm-commits at lists.llvm.org
Wed May 19 12:39:45 PDT 2021
Author: Arthur Eubanks
Date: 2021-05-19T12:39:37-07:00
New Revision: 28b9771472fa3df2568b6c5de1a9726680d940d3
URL: https://github.com/llvm/llvm-project/commit/28b9771472fa3df2568b6c5de1a9726680d940d3
DIFF: https://github.com/llvm/llvm-project/commit/28b9771472fa3df2568b6c5de1a9726680d940d3.diff
LOG: [OpaquePtr] Make GEPs work with opaque pointers
No verifier changes needed, the verifier currently doesn't check that
the pointer operand's pointee type matches the GEP type. There is a
similar check in GetElementPtrInst::Create() though.
Reviewed By: dblaikie
Differential Revision: https://reviews.llvm.org/D102744
Added:
Modified:
llvm/include/llvm/IR/Instructions.h
llvm/lib/AsmParser/LLParser.cpp
llvm/lib/Bitcode/Reader/BitcodeReader.cpp
llvm/test/Assembler/opaque-ptr.ll
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h
index 52b8bf5cf0803..1ab5ac028ac96 100644
--- a/llvm/include/llvm/IR/Instructions.h
+++ b/llvm/include/llvm/IR/Instructions.h
@@ -933,13 +933,13 @@ class GetElementPtrInst : public Instruction {
const Twine &NameStr = "",
Instruction *InsertBefore = nullptr) {
unsigned Values = 1 + unsigned(IdxList.size());
- if (!PointeeType)
+ if (!PointeeType) {
PointeeType =
cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
- else
- assert(
- PointeeType ==
- cast<PointerType>(Ptr->getType()->getScalarType())->getElementType());
+ } else {
+ assert(cast<PointerType>(Ptr->getType()->getScalarType())
+ ->isOpaqueOrPointeeTypeMatches(PointeeType));
+ }
return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
NameStr, InsertBefore);
}
@@ -949,13 +949,13 @@ class GetElementPtrInst : public Instruction {
const Twine &NameStr,
BasicBlock *InsertAtEnd) {
unsigned Values = 1 + unsigned(IdxList.size());
- if (!PointeeType)
+ if (!PointeeType) {
PointeeType =
cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
- else
- assert(
- PointeeType ==
- cast<PointerType>(Ptr->getType()->getScalarType())->getElementType());
+ } else {
+ assert(cast<PointerType>(Ptr->getType()->getScalarType())
+ ->isOpaqueOrPointeeTypeMatches(PointeeType));
+ }
return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
NameStr, InsertAtEnd);
}
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 2f85e8de575f9..a4bb423a94233 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -7741,7 +7741,7 @@ int LLParser::parseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
if (!BasePointerType)
return error(Loc, "base of getelementptr must be a pointer");
- if (Ty != BasePointerType->getElementType()) {
+ if (!BasePointerType->isOpaqueOrPointeeTypeMatches(Ty)) {
return error(
ExplicitTypeLoc,
typeComparisonErrorMessage(
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index b2794b38bd4ea..c61d66f8f4a44 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -4168,7 +4168,8 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
if (!Ty) {
std::tie(FullTy, Ty) =
getPointerElementTypes(FullBaseTy->getScalarType());
- } else if (Ty != getPointerElementFlatType(FullBaseTy->getScalarType()))
+ } else if (!cast<PointerType>(FullBaseTy->getScalarType())
+ ->isOpaqueOrPointeeTypeMatches(Ty))
return error(
"Explicit gep type does not match pointee type of pointer operand");
diff --git a/llvm/test/Assembler/opaque-ptr.ll b/llvm/test/Assembler/opaque-ptr.ll
index 95a5a53e80b71..1fb0c5da8eafd 100644
--- a/llvm/test/Assembler/opaque-ptr.ll
+++ b/llvm/test/Assembler/opaque-ptr.ll
@@ -40,3 +40,11 @@ define void @store(ptr %a, i32 %i) {
store i32 %i, ptr %a
ret void
}
+
+; CHECK: define void @gep(ptr %a)
+; CHECK: %b = getelementptr i8, ptr %a, i32 2
+; CHECK: ret void
+define void @gep(ptr %a) {
+ %b = getelementptr i8, ptr %a, i32 2
+ ret void
+}
More information about the llvm-commits
mailing list