Skip to content

Commit ad6df41

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 ad6df41

File tree

1 file changed

+46
-65
lines changed

1 file changed

+46
-65
lines changed

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

Lines changed: 46 additions & 65 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,12 @@ 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(),
460+
getPtrTy(), getPtrTy(),
461+
getPtrTy());
463462
return ImageTy;
464463
}
465464

466-
PointerType *getDeviceImagePtrTy() {
467-
return PointerType::getUnqual(getDeviceImageTy());
468-
}
469-
470465
// struct __tgt_bin_desc {
471466
// int32_t NumDeviceImages;
472467
// __tgt_device_image *DeviceImages;
@@ -476,15 +471,11 @@ class BinaryWrapper {
476471
StructType *getBinDescTy() {
477472
if (!DescTy)
478473
DescTy = StructType::create("__tgt_bin_desc", Type::getInt32Ty(C),
479-
getDeviceImagePtrTy(), getEntryPtrTy(),
480-
getEntryPtrTy());
474+
getPtrTy(), getPtrTy(),
475+
getPtrTy());
481476
return DescTy;
482477
}
483478

484-
PointerType *getBinDescPtrTy() {
485-
return PointerType::getUnqual(getBinDescTy());
486-
}
487-
488479
// DeviceImageStructVersion change log:
489480
// -- version 2: updated to PI 1.2 binary image format
490481
const uint16_t DeviceImageStructVersion = 2;
@@ -505,8 +496,8 @@ class BinaryWrapper {
505496
if (!SyclPropTy) {
506497
SyclPropTy = StructType::create(
507498
{
508-
PointerType::getUnqual(C), // Name
509-
PointerType::getUnqual(C), // ValAddr
499+
getPtrTy(), // Name
500+
getPtrTy(), // ValAddr
510501
Type::getInt32Ty(C), // Type
511502
Type::getInt64Ty(C) // ValSize
512503
},
@@ -515,10 +506,6 @@ class BinaryWrapper {
515506
return SyclPropTy;
516507
}
517508

518-
PointerType *getSyclPropPtrTy() {
519-
return PointerType::getUnqual(getSyclPropTy());
520-
}
521-
522509
// struct _pi_device_binary_property_set_struct {
523510
// char *Name;
524511
// _pi_device_binary_property_struct* PropertiesBegin;
@@ -529,19 +516,15 @@ class BinaryWrapper {
529516
if (!SyclPropSetTy) {
530517
SyclPropSetTy = StructType::create(
531518
{
532-
PointerType::getUnqual(C), // Name
533-
getSyclPropPtrTy(), // PropertiesBegin
534-
getSyclPropPtrTy() // PropertiesEnd
519+
getPtrTy(), // Name
520+
getPtrTy(), // PropertiesBegin
521+
getPtrTy() // PropertiesEnd
535522
},
536523
"_pi_device_binary_property_set_struct");
537524
}
538525
return SyclPropSetTy;
539526
}
540527

541-
PointerType *getSyclPropSetPtrTy() {
542-
return PointerType::getUnqual(getSyclPropSetTy());
543-
}
544-
545528
// SYCL specific image descriptor type.
546529
// struct __tgt_device_image {
547530
// /// version of this structure - for backward compatibility;
@@ -584,25 +567,27 @@ class BinaryWrapper {
584567
Type::getInt16Ty(C), // Version
585568
Type::getInt8Ty(C), // OffloadKind
586569
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
570+
getPtrTy(), // DeviceTargetSpec
571+
getPtrTy(), // CompileOptions
572+
getPtrTy(), // LinkOptions
573+
getPtrTy(), // ManifestStart
574+
getPtrTy(), // ManifestEnd
575+
getPtrTy(), // ImageStart
576+
getPtrTy(), // ImageEnd
577+
getPtrTy(), // EntriesBegin
578+
getPtrTy(), // EntriesEnd
579+
getPtrTy(), // PropertySetBegin
580+
getPtrTy() // PropertySetEnd
598581
},
599582
"__tgt_device_image");
600583
}
601584
return SyclImageTy;
602585
}
603586

604-
PointerType *getSyclDeviceImagePtrTy() {
605-
return PointerType::getUnqual(getSyclDeviceImageTy());
587+
PointerType *getPtrTy() {
588+
PointerType *&Ty = PtrTy;
589+
Ty = Ty ? Ty : getPtrTy();
590+
return Ty;
606591
}
607592

608593
const uint16_t BinDescStructVersion = 1;
@@ -625,19 +610,15 @@ class BinaryWrapper {
625610
{
626611
Type::getInt16Ty(C), // Version
627612
Type::getInt16Ty(C), // NumDeviceImages
628-
getSyclDeviceImagePtrTy(), // DeviceImages
629-
getEntryPtrTy(), // HostEntriesBegin
630-
getEntryPtrTy() // HostEntriesEnd
613+
getPtrTy(), // DeviceImages
614+
getPtrTy(), // HostEntriesBegin
615+
getPtrTy() // HostEntriesEnd
631616
},
632617
"__tgt_bin_desc");
633618
}
634619
return SyclDescTy;
635620
}
636621

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

@@ -651,9 +632,9 @@ class BinaryWrapper {
651632
Function *addDeclarationForNativeCPU(StringRef Name) {
652633
static FunctionType *NativeCPUFuncTy = FunctionType::get(
653634
Type::getVoidTy(C),
654-
{PointerType::getUnqual(C), PointerType::getUnqual(C)}, false);
635+
{getPtrTy(), getPtrTy()}, false);
655636
static FunctionType *NativeCPUBuiltinTy = FunctionType::get(
656-
PointerType::getUnqual(C), {PointerType::getUnqual(C)}, false);
637+
getPtrTy(), {getPtrTy()}, false);
657638
FunctionType *FTy;
658639
if (Name.starts_with("__dpcpp_nativecpu"))
659640
FTy = NativeCPUBuiltinTy;
@@ -679,7 +660,7 @@ class BinaryWrapper {
679660
// unsigned char *kernel_ptr;
680661
// };
681662
StructType *NCPUEntryT = StructType::create(
682-
{PointerType::getUnqual(C), PointerType::getUnqual(C)},
663+
{getPtrTy(), getPtrTy()},
683664
"__nativecpu_entry");
684665
SmallVector<Constant *, 5> NativeCPUEntries;
685666
for (line_iterator LI(*MB); !LI.is_at_eof(); ++LI) {
@@ -692,7 +673,7 @@ class BinaryWrapper {
692673
// Add an empty entry that we use as end iterator
693674
static auto *NativeCPUEndStr =
694675
addStringToModule("__nativecpu_end", "__ncpu_end_str");
695-
auto *NullPtr = llvm::ConstantPointerNull::get(PointerType::getUnqual(C));
676+
auto *NullPtr = llvm::ConstantPointerNull::get(getPtrTy());
696677
NativeCPUEntries.push_back(
697678
ConstantStruct::get(NCPUEntryT, {NativeCPUEndStr, NullPtr}));
698679

@@ -790,13 +771,13 @@ class BinaryWrapper {
790771
Expected<std::pair<Constant *, Constant *>>
791772
addSYCLOffloadEntriesToModule(StringRef EntriesFile) {
792773
if (EntriesFile.empty() && !MySymPropReader) {
793-
auto *NullPtr = Constant::getNullValue(getEntryPtrTy());
774+
auto *NullPtr = Constant::getNullValue(getPtrTy());
794775
return std::pair<Constant *, Constant *>(NullPtr, NullPtr);
795776
}
796777

797778
auto *Zero = ConstantInt::get(getSizeTTy(), 0u);
798779
auto *i32Zero = ConstantInt::get(Type::getInt32Ty(C), 0u);
799-
auto *NullPtr = Constant::getNullValue(PointerType::getUnqual(C));
780+
auto *NullPtr = Constant::getNullValue(getPtrTy());
800781

801782
std::vector<Constant *> EntriesInits;
802783
// Only the name field is used for SYCL now, others are for future OpenMP
@@ -851,7 +832,7 @@ class BinaryWrapper {
851832
switch (Prop.second.getType()) {
852833
case llvm::util::PropertyValue::UINT32: {
853834
// for known scalar types ValAddr is null, ValSize keeps the value
854-
PropValAddr = Constant::getNullValue(PointerType::getUnqual(C));
835+
PropValAddr = Constant::getNullValue(getPtrTy());
855836
PropValSize =
856837
ConstantInt::get(Type::getInt64Ty(C), Prop.second.asUint32());
857838
break;
@@ -917,7 +898,7 @@ class BinaryWrapper {
917898
PropRegistry = MySymPropReader->getPropRegistry();
918899
} else {
919900
if (PropRegistryFile.empty()) {
920-
auto *NullPtr = Constant::getNullValue(llvm::PointerType::getUnqual(C));
901+
auto *NullPtr = Constant::getNullValue(getPtrTy());
921902
return std::pair<Constant *, Constant *>(NullPtr, NullPtr);
922903
}
923904
// load the property registry file
@@ -1036,12 +1017,12 @@ class BinaryWrapper {
10361017
}
10371018
} else {
10381019
// Host entry table is not used in SYCL
1039-
EntriesB = Constant::getNullValue(getEntryPtrTy());
1040-
EntriesE = Constant::getNullValue(getEntryPtrTy());
1020+
EntriesB = Constant::getNullValue(getPtrTy());
1021+
EntriesE = Constant::getNullValue(getPtrTy());
10411022
}
10421023

10431024
auto *Zero = ConstantInt::get(getSizeTTy(), 0u);
1044-
auto *NullPtr = Constant::getNullValue(PointerType::getUnqual(C));
1025+
auto *NullPtr = Constant::getNullValue(getPtrTy());
10451026
Constant *ZeroZero[] = {Zero, Zero};
10461027

10471028
// Create initializer for the images array.
@@ -1272,7 +1253,7 @@ class BinaryWrapper {
12721253
// Get RegFuncName function declaration.
12731254
auto *RegFuncTy = FunctionType::get(
12741255
Type::getVoidTy(C),
1275-
Kind == OffloadKind::SYCL ? getSyclBinDescPtrTy() : getBinDescPtrTy(),
1256+
getPtrTy(),
12761257
/*isVarArg=*/false);
12771258
FunctionCallee RegFuncC =
12781259
M.getOrInsertFunction(Kind == OffloadKind::SYCL ? "__sycl_register_lib"
@@ -1303,7 +1284,7 @@ class BinaryWrapper {
13031284
// Get UnregFuncName function declaration.
13041285
auto *UnRegFuncTy = FunctionType::get(
13051286
Type::getVoidTy(C),
1306-
Kind == OffloadKind::SYCL ? getSyclBinDescPtrTy() : getBinDescPtrTy(),
1287+
getPtrTy(),
13071288
/*isVarArg=*/false);
13081289
FunctionCallee UnRegFuncC = M.getOrInsertFunction(
13091290
Kind == OffloadKind::SYCL ? "__sycl_unregister_lib"

0 commit comments

Comments
 (0)