Skip to content

Commit 70459b6

Browse files
committed
Cleanup typed pointer holdovers in clang-offload-wrapper
The overload llvm::PointerType::getUnqual(ElementTy *) is now simply a wrapper around `llvm::PointerType::getUnqual(Context &)` i.e. PointerType *llvm::PointerType::getUnqual(ElementTy *E) { return getUnqual(E->getContext()); } Since there's no longer a possible distinction between the returned types, we can get rid of all the typed getters and simply cached the unqualified opaque pointer type on first use.
1 parent c018b49 commit 70459b6

File tree

1 file changed

+56
-83
lines changed

1 file changed

+56
-83
lines changed

clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp

Lines changed: 56 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ class BinaryWrapper {
360360
StructType *SyclDescTy = nullptr;
361361
StructType *SyclPropSetTy = nullptr;
362362
StructType *SyclPropTy = nullptr;
363+
PointerType *PtrTy = nullptr;
363364

364365
/// Records all added device binary images per offload kind.
365366
llvm::DenseMap<OffloadKind, std::unique_ptr<SameKindPack>> Packs;
@@ -392,7 +393,7 @@ class BinaryWrapper {
392393
std::unique_ptr<SymPropReader> MySymPropReader;
393394

394395
IntegerType *getSizeTTy() {
395-
switch (M.getDataLayout().getPointerTypeSize(PointerType::getUnqual(C))) {
396+
switch (M.getDataLayout().getPointerTypeSize(getPtrTy())) {
396397
case 4u:
397398
return Type::getInt32Ty(C);
398399
case 8u:
@@ -409,7 +410,7 @@ class BinaryWrapper {
409410
std::pair<Constant *, Constant *>
410411
addStructArrayToModule(ArrayRef<Constant *> ArrayData, Type *ElemTy) {
411412

412-
auto *PtrTy = llvm::PointerType::getUnqual(C);
413+
auto *PtrTy = getPtrTy();
413414

414415
if (ArrayData.size() == 0) {
415416
auto *NullPtr = Constant::getNullValue(PtrTy);
@@ -441,14 +442,12 @@ class BinaryWrapper {
441442
// };
442443
StructType *getEntryTy() {
443444
if (!EntryTy)
444-
EntryTy = StructType::create("__tgt_offload_entry", PointerType::getUnqual(C),
445-
PointerType::getUnqual(C), getSizeTTy(),
445+
EntryTy = StructType::create("__tgt_offload_entry", getPtrTy(),
446+
getPtrTy(), getSizeTTy(),
446447
Type::getInt32Ty(C), Type::getInt32Ty(C));
447448
return EntryTy;
448449
}
449450

450-
PointerType *getEntryPtrTy() { return PointerType::getUnqual(getEntryTy()); }
451-
452451
// struct __tgt_device_image {
453452
// void *ImageStart;
454453
// void *ImageEnd;
@@ -457,16 +456,11 @@ class BinaryWrapper {
457456
// };
458457
StructType *getDeviceImageTy() {
459458
if (!ImageTy)
460-
ImageTy = StructType::create("__tgt_device_image", PointerType::getUnqual(C),
461-
PointerType::getUnqual(C), getEntryPtrTy(),
462-
getEntryPtrTy());
459+
ImageTy = StructType::create("__tgt_device_image", getPtrTy(), getPtrTy(),
460+
getPtrTy(), getPtrTy());
463461
return ImageTy;
464462
}
465463

466-
PointerType *getDeviceImagePtrTy() {
467-
return PointerType::getUnqual(getDeviceImageTy());
468-
}
469-
470464
// struct __tgt_bin_desc {
471465
// int32_t NumDeviceImages;
472466
// __tgt_device_image *DeviceImages;
@@ -476,15 +470,10 @@ class BinaryWrapper {
476470
StructType *getBinDescTy() {
477471
if (!DescTy)
478472
DescTy = StructType::create("__tgt_bin_desc", Type::getInt32Ty(C),
479-
getDeviceImagePtrTy(), getEntryPtrTy(),
480-
getEntryPtrTy());
473+
getPtrTy(), getPtrTy(), getPtrTy());
481474
return DescTy;
482475
}
483476

484-
PointerType *getBinDescPtrTy() {
485-
return PointerType::getUnqual(getBinDescTy());
486-
}
487-
488477
// DeviceImageStructVersion change log:
489478
// -- version 2: updated to PI 1.2 binary image format
490479
const uint16_t DeviceImageStructVersion = 2;
@@ -505,20 +494,16 @@ class BinaryWrapper {
505494
if (!SyclPropTy) {
506495
SyclPropTy = StructType::create(
507496
{
508-
PointerType::getUnqual(C), // Name
509-
PointerType::getUnqual(C), // ValAddr
510-
Type::getInt32Ty(C), // Type
511-
Type::getInt64Ty(C) // ValSize
497+
getPtrTy(), // Name
498+
getPtrTy(), // ValAddr
499+
Type::getInt32Ty(C), // Type
500+
Type::getInt64Ty(C) // ValSize
512501
},
513502
"_pi_device_binary_property_struct");
514503
}
515504
return SyclPropTy;
516505
}
517506

518-
PointerType *getSyclPropPtrTy() {
519-
return PointerType::getUnqual(getSyclPropTy());
520-
}
521-
522507
// struct _pi_device_binary_property_set_struct {
523508
// char *Name;
524509
// _pi_device_binary_property_struct* PropertiesBegin;
@@ -529,19 +514,15 @@ class BinaryWrapper {
529514
if (!SyclPropSetTy) {
530515
SyclPropSetTy = StructType::create(
531516
{
532-
PointerType::getUnqual(C), // Name
533-
getSyclPropPtrTy(), // PropertiesBegin
534-
getSyclPropPtrTy() // PropertiesEnd
517+
getPtrTy(), // Name
518+
getPtrTy(), // PropertiesBegin
519+
getPtrTy() // PropertiesEnd
535520
},
536521
"_pi_device_binary_property_set_struct");
537522
}
538523
return SyclPropSetTy;
539524
}
540525

541-
PointerType *getSyclPropSetPtrTy() {
542-
return PointerType::getUnqual(getSyclPropSetTy());
543-
}
544-
545526
// SYCL specific image descriptor type.
546527
// struct __tgt_device_image {
547528
// /// version of this structure - for backward compatibility;
@@ -581,28 +562,30 @@ class BinaryWrapper {
581562
if (!SyclImageTy) {
582563
SyclImageTy = StructType::create(
583564
{
584-
Type::getInt16Ty(C), // Version
585-
Type::getInt8Ty(C), // OffloadKind
586-
Type::getInt8Ty(C), // Format
587-
PointerType::getUnqual(C), // DeviceTargetSpec
588-
PointerType::getUnqual(C), // CompileOptions
589-
PointerType::getUnqual(C), // LinkOptions
590-
PointerType::getUnqual(C), // ManifestStart
591-
PointerType::getUnqual(C), // ManifestEnd
592-
PointerType::getUnqual(C), // ImageStart
593-
PointerType::getUnqual(C), // ImageEnd
594-
getEntryPtrTy(), // EntriesBegin
595-
getEntryPtrTy(), // EntriesEnd
596-
getSyclPropSetPtrTy(), // PropertySetBegin
597-
getSyclPropSetPtrTy() // PropertySetEnd
565+
Type::getInt16Ty(C), // Version
566+
Type::getInt8Ty(C), // OffloadKind
567+
Type::getInt8Ty(C), // Format
568+
getPtrTy(), // DeviceTargetSpec
569+
getPtrTy(), // CompileOptions
570+
getPtrTy(), // LinkOptions
571+
getPtrTy(), // ManifestStart
572+
getPtrTy(), // ManifestEnd
573+
getPtrTy(), // ImageStart
574+
getPtrTy(), // ImageEnd
575+
getPtrTy(), // EntriesBegin
576+
getPtrTy(), // EntriesEnd
577+
getPtrTy(), // PropertySetBegin
578+
getPtrTy() // PropertySetEnd
598579
},
599580
"__tgt_device_image");
600581
}
601582
return SyclImageTy;
602583
}
603584

604-
PointerType *getSyclDeviceImagePtrTy() {
605-
return PointerType::getUnqual(getSyclDeviceImageTy());
585+
PointerType *getPtrTy() {
586+
PointerType *&Ty = PtrTy;
587+
Ty = Ty ? Ty : getPtrTy();
588+
return Ty;
606589
}
607590

608591
const uint16_t BinDescStructVersion = 1;
@@ -623,21 +606,17 @@ class BinaryWrapper {
623606
if (!SyclDescTy) {
624607
SyclDescTy = StructType::create(
625608
{
626-
Type::getInt16Ty(C), // Version
627-
Type::getInt16Ty(C), // NumDeviceImages
628-
getSyclDeviceImagePtrTy(), // DeviceImages
629-
getEntryPtrTy(), // HostEntriesBegin
630-
getEntryPtrTy() // HostEntriesEnd
609+
Type::getInt16Ty(C), // Version
610+
Type::getInt16Ty(C), // NumDeviceImages
611+
getPtrTy(), // DeviceImages
612+
getPtrTy(), // HostEntriesBegin
613+
getPtrTy() // HostEntriesEnd
631614
},
632615
"__tgt_bin_desc");
633616
}
634617
return SyclDescTy;
635618
}
636619

637-
PointerType *getSyclBinDescPtrTy() {
638-
return PointerType::getUnqual(getSyclBinDescTy());
639-
}
640-
641620
Expected<MemoryBuffer *> loadFile(llvm::StringRef Name) {
642621
auto InputOrErr = MemoryBuffer::getFileOrSTDIN(Name);
643622

@@ -649,11 +628,10 @@ class BinaryWrapper {
649628
}
650629

651630
Function *addDeclarationForNativeCPU(StringRef Name) {
652-
static FunctionType *NativeCPUFuncTy = FunctionType::get(
653-
Type::getVoidTy(C),
654-
{PointerType::getUnqual(C), PointerType::getUnqual(C)}, false);
655-
static FunctionType *NativeCPUBuiltinTy = FunctionType::get(
656-
PointerType::getUnqual(C), {PointerType::getUnqual(C)}, false);
631+
static FunctionType *NativeCPUFuncTy =
632+
FunctionType::get(Type::getVoidTy(C), {getPtrTy(), getPtrTy()}, false);
633+
static FunctionType *NativeCPUBuiltinTy =
634+
FunctionType::get(getPtrTy(), {getPtrTy()}, false);
657635
FunctionType *FTy;
658636
if (Name.starts_with("__dpcpp_nativecpu"))
659637
FTy = NativeCPUBuiltinTy;
@@ -678,9 +656,8 @@ class BinaryWrapper {
678656
// char *kernelname;
679657
// unsigned char *kernel_ptr;
680658
// };
681-
StructType *NCPUEntryT = StructType::create(
682-
{PointerType::getUnqual(C), PointerType::getUnqual(C)},
683-
"__nativecpu_entry");
659+
StructType *NCPUEntryT =
660+
StructType::create({getPtrTy(), getPtrTy()}, "__nativecpu_entry");
684661
SmallVector<Constant *, 5> NativeCPUEntries;
685662
for (line_iterator LI(*MB); !LI.is_at_eof(); ++LI) {
686663
auto *NewDecl = addDeclarationForNativeCPU(*LI);
@@ -692,7 +669,7 @@ class BinaryWrapper {
692669
// Add an empty entry that we use as end iterator
693670
static auto *NativeCPUEndStr =
694671
addStringToModule("__nativecpu_end", "__ncpu_end_str");
695-
auto *NullPtr = llvm::ConstantPointerNull::get(PointerType::getUnqual(C));
672+
auto *NullPtr = llvm::ConstantPointerNull::get(getPtrTy());
696673
NativeCPUEntries.push_back(
697674
ConstantStruct::get(NCPUEntryT, {NativeCPUEndStr, NullPtr}));
698675

@@ -790,13 +767,13 @@ class BinaryWrapper {
790767
Expected<std::pair<Constant *, Constant *>>
791768
addSYCLOffloadEntriesToModule(StringRef EntriesFile) {
792769
if (EntriesFile.empty() && !MySymPropReader) {
793-
auto *NullPtr = Constant::getNullValue(getEntryPtrTy());
770+
auto *NullPtr = Constant::getNullValue(getPtrTy());
794771
return std::pair<Constant *, Constant *>(NullPtr, NullPtr);
795772
}
796773

797774
auto *Zero = ConstantInt::get(getSizeTTy(), 0u);
798775
auto *i32Zero = ConstantInt::get(Type::getInt32Ty(C), 0u);
799-
auto *NullPtr = Constant::getNullValue(PointerType::getUnqual(C));
776+
auto *NullPtr = Constant::getNullValue(getPtrTy());
800777

801778
std::vector<Constant *> EntriesInits;
802779
// Only the name field is used for SYCL now, others are for future OpenMP
@@ -851,7 +828,7 @@ class BinaryWrapper {
851828
switch (Prop.second.getType()) {
852829
case llvm::util::PropertyValue::UINT32: {
853830
// for known scalar types ValAddr is null, ValSize keeps the value
854-
PropValAddr = Constant::getNullValue(PointerType::getUnqual(C));
831+
PropValAddr = Constant::getNullValue(getPtrTy());
855832
PropValSize =
856833
ConstantInt::get(Type::getInt64Ty(C), Prop.second.asUint32());
857834
break;
@@ -917,7 +894,7 @@ class BinaryWrapper {
917894
PropRegistry = MySymPropReader->getPropRegistry();
918895
} else {
919896
if (PropRegistryFile.empty()) {
920-
auto *NullPtr = Constant::getNullValue(llvm::PointerType::getUnqual(C));
897+
auto *NullPtr = Constant::getNullValue(getPtrTy());
921898
return std::pair<Constant *, Constant *>(NullPtr, NullPtr);
922899
}
923900
// load the property registry file
@@ -1036,12 +1013,12 @@ class BinaryWrapper {
10361013
}
10371014
} else {
10381015
// Host entry table is not used in SYCL
1039-
EntriesB = Constant::getNullValue(getEntryPtrTy());
1040-
EntriesE = Constant::getNullValue(getEntryPtrTy());
1016+
EntriesB = Constant::getNullValue(getPtrTy());
1017+
EntriesE = Constant::getNullValue(getPtrTy());
10411018
}
10421019

10431020
auto *Zero = ConstantInt::get(getSizeTTy(), 0u);
1044-
auto *NullPtr = Constant::getNullValue(PointerType::getUnqual(C));
1021+
auto *NullPtr = Constant::getNullValue(getPtrTy());
10451022
Constant *ZeroZero[] = {Zero, Zero};
10461023

10471024
// Create initializer for the images array.
@@ -1270,10 +1247,8 @@ class BinaryWrapper {
12701247
Func->setSection(".text.startup");
12711248

12721249
// Get RegFuncName function declaration.
1273-
auto *RegFuncTy = FunctionType::get(
1274-
Type::getVoidTy(C),
1275-
Kind == OffloadKind::SYCL ? getSyclBinDescPtrTy() : getBinDescPtrTy(),
1276-
/*isVarArg=*/false);
1250+
auto *RegFuncTy = FunctionType::get(Type::getVoidTy(C), getPtrTy(),
1251+
/*isVarArg=*/false);
12771252
FunctionCallee RegFuncC =
12781253
M.getOrInsertFunction(Kind == OffloadKind::SYCL ? "__sycl_register_lib"
12791254
: "__tgt_register_lib",
@@ -1301,10 +1276,8 @@ class BinaryWrapper {
13011276
Func->setSection(".text.startup");
13021277

13031278
// Get UnregFuncName function declaration.
1304-
auto *UnRegFuncTy = FunctionType::get(
1305-
Type::getVoidTy(C),
1306-
Kind == OffloadKind::SYCL ? getSyclBinDescPtrTy() : getBinDescPtrTy(),
1307-
/*isVarArg=*/false);
1279+
auto *UnRegFuncTy = FunctionType::get(Type::getVoidTy(C), getPtrTy(),
1280+
/*isVarArg=*/false);
13081281
FunctionCallee UnRegFuncC = M.getOrInsertFunction(
13091282
Kind == OffloadKind::SYCL ? "__sycl_unregister_lib"
13101283
: "__tgt_unregister_lib",

0 commit comments

Comments
 (0)