Skip to content

Commit 4c8174f

Browse files
committed
[OpaquePtr] Introduce option to force all pointers to be opaque pointers
We don't want to start updating tests to use opaque pointers until we're close to the opaque pointer transition. However, before the transition we want to run tests as if pointers are opaque pointers to see if there are any crashes. At some point when we have a flag to only create opaque pointers in the bitcode and textual IR readers, and when we have fixed all places that try to read a pointee type, this flag will be useless. However, until then, this can help us find issues more easily. Since the cl::opt is read into LLVMContext, we need to make sure LLVMContext is created after cl::ParseCommandLineOptions(). Previously ValueEnumerator would visit the value types of global values via the pointer type, but with opaque pointers we have to manually visit the value type. Reviewed By: nikic, dexonsmith Differential Revision: https://reviews.llvm.org/D103503
1 parent 7110510 commit 4c8174f

File tree

8 files changed

+36
-25
lines changed

8 files changed

+36
-25
lines changed

llvm/lib/Bitcode/Writer/ValueEnumerator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ ValueEnumerator::ValueEnumerator(const Module &M,
371371
// Enumerate the functions.
372372
for (const Function & F : M) {
373373
EnumerateValue(&F);
374+
EnumerateType(F.getValueType());
374375
EnumerateAttributes(F.getAttributes());
375376
}
376377

llvm/lib/IR/LLVMContextImpl.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,29 @@
1515
#include "llvm/IR/Module.h"
1616
#include "llvm/IR/OptBisect.h"
1717
#include "llvm/IR/Type.h"
18+
#include "llvm/Support/CommandLine.h"
1819
#include "llvm/Support/ManagedStatic.h"
1920
#include <cassert>
2021
#include <utility>
2122

2223
using namespace llvm;
2324

25+
static cl::opt<bool>
26+
ForceOpaquePointersCL("force-opaque-pointers",
27+
cl::desc("Force all pointers to be opaque pointers"),
28+
cl::init(false));
29+
2430
LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
25-
: DiagHandler(std::make_unique<DiagnosticHandler>()),
26-
VoidTy(C, Type::VoidTyID),
27-
LabelTy(C, Type::LabelTyID),
28-
HalfTy(C, Type::HalfTyID),
29-
BFloatTy(C, Type::BFloatTyID),
30-
FloatTy(C, Type::FloatTyID),
31-
DoubleTy(C, Type::DoubleTyID),
32-
MetadataTy(C, Type::MetadataTyID),
33-
TokenTy(C, Type::TokenTyID),
34-
X86_FP80Ty(C, Type::X86_FP80TyID),
35-
FP128Ty(C, Type::FP128TyID),
36-
PPC_FP128Ty(C, Type::PPC_FP128TyID),
37-
X86_MMXTy(C, Type::X86_MMXTyID),
38-
X86_AMXTy(C, Type::X86_AMXTyID),
39-
Int1Ty(C, 1),
40-
Int8Ty(C, 8),
41-
Int16Ty(C, 16),
42-
Int32Ty(C, 32),
43-
Int64Ty(C, 64),
44-
Int128Ty(C, 128) {}
31+
: DiagHandler(std::make_unique<DiagnosticHandler>()),
32+
VoidTy(C, Type::VoidTyID), LabelTy(C, Type::LabelTyID),
33+
HalfTy(C, Type::HalfTyID), BFloatTy(C, Type::BFloatTyID),
34+
FloatTy(C, Type::FloatTyID), DoubleTy(C, Type::DoubleTyID),
35+
MetadataTy(C, Type::MetadataTyID), TokenTy(C, Type::TokenTyID),
36+
X86_FP80Ty(C, Type::X86_FP80TyID), FP128Ty(C, Type::FP128TyID),
37+
PPC_FP128Ty(C, Type::PPC_FP128TyID), X86_MMXTy(C, Type::X86_MMXTyID),
38+
X86_AMXTy(C, Type::X86_AMXTyID), Int1Ty(C, 1), Int8Ty(C, 8),
39+
Int16Ty(C, 16), Int32Ty(C, 32), Int64Ty(C, 64), Int128Ty(C, 128),
40+
ForceOpaquePointers(ForceOpaquePointersCL) {}
4541

4642
LLVMContextImpl::~LLVMContextImpl() {
4743
// NOTE: We need to delete the contents of OwnedModules, but Module's dtor

llvm/lib/IR/LLVMContextImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,7 @@ class LLVMContextImpl {
14471447
DenseMap<std::pair<Type *, ElementCount>, VectorType*> VectorTypes;
14481448
// TODO: clean up the following after we no longer support non-opaque pointer
14491449
// types.
1450+
bool ForceOpaquePointers;
14501451
DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
14511452
DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
14521453

llvm/lib/IR/Type.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
#include "llvm/IR/Value.h"
2626
#include "llvm/Support/Casting.h"
2727
#include "llvm/Support/MathExtras.h"
28-
#include "llvm/Support/raw_ostream.h"
2928
#include "llvm/Support/TypeSize.h"
29+
#include "llvm/Support/raw_ostream.h"
3030
#include <cassert>
3131
#include <utility>
3232

@@ -690,6 +690,9 @@ PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
690690

691691
LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
692692

693+
if (CImpl->ForceOpaquePointers)
694+
return get(EltTy->getContext(), AddressSpace);
695+
693696
// Since AddressSpace #0 is the common case, we special case it.
694697
PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
695698
: CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];

llvm/test/Other/force-opaque-ptrs.ll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
; RUN: llvm-as --force-opaque-pointers < %s | llvm-dis | FileCheck %s
2+
; RUN: llvm-as < %s | llvm-dis --force-opaque-pointers | FileCheck %s
3+
; RUN: opt --force-opaque-pointers < %s -S | FileCheck %s
4+
5+
; CHECK: define void @f(ptr %p)
6+
; CHECK: ret void
7+
define void @f(i32* %p) {
8+
ret void
9+
}

llvm/tools/llvm-as/llvm-as.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ static void WriteOutputFile(const Module *M, const ModuleSummaryIndex *Index) {
115115

116116
int main(int argc, char **argv) {
117117
InitLLVM X(argc, argv);
118-
LLVMContext Context;
119118
cl::HideUnrelatedOptions(AsCat);
120119
cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
120+
LLVMContext Context;
121121

122122
// Parse the file now...
123123
SMDiagnostic Err;

llvm/tools/llvm-dis/llvm-dis.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,11 @@ int main(int argc, char **argv) {
151151

152152
ExitOnErr.setBanner(std::string(argv[0]) + ": error: ");
153153

154+
cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
155+
154156
LLVMContext Context;
155157
Context.setDiagnosticHandler(
156158
std::make_unique<LLVMDisDiagnosticHandler>(argv[0]));
157-
cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
158159

159160
if (InputFilenames.size() < 1) {
160161
InputFilenames.push_back("-");

llvm/tools/opt/opt.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,6 @@ int main(int argc, char **argv) {
552552
// Enable debug stream buffering.
553553
EnableDebugBuffering = true;
554554

555-
LLVMContext Context;
556-
557555
InitializeAllTargets();
558556
InitializeAllTargetMCs();
559557
InitializeAllAsmPrinters();
@@ -607,6 +605,8 @@ int main(int argc, char **argv) {
607605
cl::ParseCommandLineOptions(argc, argv,
608606
"llvm .bc -> .bc modular optimizer and analysis printer\n");
609607

608+
LLVMContext Context;
609+
610610
if (AnalyzeOnly && NoOutput) {
611611
errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
612612
return 1;

0 commit comments

Comments
 (0)