Skip to content

[NFC] Cleanup typed pointer holdovers in clang-offload-wrapper #19370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 56 additions & 83 deletions clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ class BinaryWrapper {
StructType *SyclDescTy = nullptr;
StructType *SyclPropSetTy = nullptr;
StructType *SyclPropTy = nullptr;
PointerType *PtrTy = nullptr;

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

IntegerType *getSizeTTy() {
switch (M.getDataLayout().getPointerTypeSize(PointerType::getUnqual(C))) {
switch (M.getDataLayout().getPointerTypeSize(getPtrTy())) {
case 4u:
return Type::getInt32Ty(C);
case 8u:
Expand All @@ -409,7 +410,7 @@ class BinaryWrapper {
std::pair<Constant *, Constant *>
addStructArrayToModule(ArrayRef<Constant *> ArrayData, Type *ElemTy) {

auto *PtrTy = llvm::PointerType::getUnqual(C);
auto *PtrTy = getPtrTy();

if (ArrayData.size() == 0) {
auto *NullPtr = Constant::getNullValue(PtrTy);
Expand Down Expand Up @@ -441,14 +442,12 @@ class BinaryWrapper {
// };
StructType *getEntryTy() {
if (!EntryTy)
EntryTy = StructType::create("__tgt_offload_entry", PointerType::getUnqual(C),
PointerType::getUnqual(C), getSizeTTy(),
EntryTy = StructType::create("__tgt_offload_entry", getPtrTy(),
getPtrTy(), getSizeTTy(),
Type::getInt32Ty(C), Type::getInt32Ty(C));
return EntryTy;
}

PointerType *getEntryPtrTy() { return PointerType::getUnqual(getEntryTy()); }

// struct __tgt_device_image {
// void *ImageStart;
// void *ImageEnd;
Expand All @@ -457,16 +456,11 @@ class BinaryWrapper {
// };
StructType *getDeviceImageTy() {
if (!ImageTy)
ImageTy = StructType::create("__tgt_device_image", PointerType::getUnqual(C),
PointerType::getUnqual(C), getEntryPtrTy(),
getEntryPtrTy());
ImageTy = StructType::create("__tgt_device_image", getPtrTy(), getPtrTy(),
getPtrTy(), getPtrTy());
return ImageTy;
}

PointerType *getDeviceImagePtrTy() {
return PointerType::getUnqual(getDeviceImageTy());
}

// struct __tgt_bin_desc {
// int32_t NumDeviceImages;
// __tgt_device_image *DeviceImages;
Expand All @@ -476,15 +470,10 @@ class BinaryWrapper {
StructType *getBinDescTy() {
if (!DescTy)
DescTy = StructType::create("__tgt_bin_desc", Type::getInt32Ty(C),
getDeviceImagePtrTy(), getEntryPtrTy(),
getEntryPtrTy());
getPtrTy(), getPtrTy(), getPtrTy());
return DescTy;
}

PointerType *getBinDescPtrTy() {
return PointerType::getUnqual(getBinDescTy());
}

// DeviceImageStructVersion change log:
// -- version 2: updated to PI 1.2 binary image format
const uint16_t DeviceImageStructVersion = 2;
Expand All @@ -505,20 +494,16 @@ class BinaryWrapper {
if (!SyclPropTy) {
SyclPropTy = StructType::create(
{
PointerType::getUnqual(C), // Name
PointerType::getUnqual(C), // ValAddr
Type::getInt32Ty(C), // Type
Type::getInt64Ty(C) // ValSize
getPtrTy(), // Name
getPtrTy(), // ValAddr
Type::getInt32Ty(C), // Type
Type::getInt64Ty(C) // ValSize
},
"_pi_device_binary_property_struct");
}
return SyclPropTy;
}

PointerType *getSyclPropPtrTy() {
return PointerType::getUnqual(getSyclPropTy());
}

// struct _pi_device_binary_property_set_struct {
// char *Name;
// _pi_device_binary_property_struct* PropertiesBegin;
Expand All @@ -529,19 +514,15 @@ class BinaryWrapper {
if (!SyclPropSetTy) {
SyclPropSetTy = StructType::create(
{
PointerType::getUnqual(C), // Name
getSyclPropPtrTy(), // PropertiesBegin
getSyclPropPtrTy() // PropertiesEnd
getPtrTy(), // Name
getPtrTy(), // PropertiesBegin
getPtrTy() // PropertiesEnd
},
"_pi_device_binary_property_set_struct");
}
return SyclPropSetTy;
}

PointerType *getSyclPropSetPtrTy() {
return PointerType::getUnqual(getSyclPropSetTy());
}

// SYCL specific image descriptor type.
// struct __tgt_device_image {
// /// version of this structure - for backward compatibility;
Expand Down Expand Up @@ -581,28 +562,30 @@ class BinaryWrapper {
if (!SyclImageTy) {
SyclImageTy = StructType::create(
{
Type::getInt16Ty(C), // Version
Type::getInt8Ty(C), // OffloadKind
Type::getInt8Ty(C), // Format
PointerType::getUnqual(C), // DeviceTargetSpec
PointerType::getUnqual(C), // CompileOptions
PointerType::getUnqual(C), // LinkOptions
PointerType::getUnqual(C), // ManifestStart
PointerType::getUnqual(C), // ManifestEnd
PointerType::getUnqual(C), // ImageStart
PointerType::getUnqual(C), // ImageEnd
getEntryPtrTy(), // EntriesBegin
getEntryPtrTy(), // EntriesEnd
getSyclPropSetPtrTy(), // PropertySetBegin
getSyclPropSetPtrTy() // PropertySetEnd
Type::getInt16Ty(C), // Version
Type::getInt8Ty(C), // OffloadKind
Type::getInt8Ty(C), // Format
getPtrTy(), // DeviceTargetSpec
getPtrTy(), // CompileOptions
getPtrTy(), // LinkOptions
getPtrTy(), // ManifestStart
getPtrTy(), // ManifestEnd
getPtrTy(), // ImageStart
getPtrTy(), // ImageEnd
getPtrTy(), // EntriesBegin
getPtrTy(), // EntriesEnd
getPtrTy(), // PropertySetBegin
getPtrTy() // PropertySetEnd
},
"__tgt_device_image");
}
return SyclImageTy;
}

PointerType *getSyclDeviceImagePtrTy() {
return PointerType::getUnqual(getSyclDeviceImageTy());
PointerType *getPtrTy() {
PointerType *&Ty = PtrTy;
Ty = Ty ? Ty : PointerType::getUnqual(C);
return Ty;
}

const uint16_t BinDescStructVersion = 1;
Expand All @@ -623,21 +606,17 @@ class BinaryWrapper {
if (!SyclDescTy) {
SyclDescTy = StructType::create(
{
Type::getInt16Ty(C), // Version
Type::getInt16Ty(C), // NumDeviceImages
getSyclDeviceImagePtrTy(), // DeviceImages
getEntryPtrTy(), // HostEntriesBegin
getEntryPtrTy() // HostEntriesEnd
Type::getInt16Ty(C), // Version
Type::getInt16Ty(C), // NumDeviceImages
getPtrTy(), // DeviceImages
getPtrTy(), // HostEntriesBegin
getPtrTy() // HostEntriesEnd
},
"__tgt_bin_desc");
}
return SyclDescTy;
}

PointerType *getSyclBinDescPtrTy() {
return PointerType::getUnqual(getSyclBinDescTy());
}

Expected<MemoryBuffer *> loadFile(llvm::StringRef Name) {
auto InputOrErr = MemoryBuffer::getFileOrSTDIN(Name);

Expand All @@ -649,11 +628,10 @@ class BinaryWrapper {
}

Function *addDeclarationForNativeCPU(StringRef Name) {
static FunctionType *NativeCPUFuncTy = FunctionType::get(
Type::getVoidTy(C),
{PointerType::getUnqual(C), PointerType::getUnqual(C)}, false);
static FunctionType *NativeCPUBuiltinTy = FunctionType::get(
PointerType::getUnqual(C), {PointerType::getUnqual(C)}, false);
static FunctionType *NativeCPUFuncTy =
FunctionType::get(Type::getVoidTy(C), {getPtrTy(), getPtrTy()}, false);
static FunctionType *NativeCPUBuiltinTy =
FunctionType::get(getPtrTy(), {getPtrTy()}, false);
FunctionType *FTy;
if (Name.starts_with("__dpcpp_nativecpu"))
FTy = NativeCPUBuiltinTy;
Expand All @@ -678,9 +656,8 @@ class BinaryWrapper {
// char *kernelname;
// unsigned char *kernel_ptr;
// };
StructType *NCPUEntryT = StructType::create(
{PointerType::getUnqual(C), PointerType::getUnqual(C)},
"__nativecpu_entry");
StructType *NCPUEntryT =
StructType::create({getPtrTy(), getPtrTy()}, "__nativecpu_entry");
SmallVector<Constant *, 5> NativeCPUEntries;
for (line_iterator LI(*MB); !LI.is_at_eof(); ++LI) {
auto *NewDecl = addDeclarationForNativeCPU(*LI);
Expand All @@ -692,7 +669,7 @@ class BinaryWrapper {
// Add an empty entry that we use as end iterator
static auto *NativeCPUEndStr =
addStringToModule("__nativecpu_end", "__ncpu_end_str");
auto *NullPtr = llvm::ConstantPointerNull::get(PointerType::getUnqual(C));
auto *NullPtr = llvm::ConstantPointerNull::get(getPtrTy());
NativeCPUEntries.push_back(
ConstantStruct::get(NCPUEntryT, {NativeCPUEndStr, NullPtr}));

Expand Down Expand Up @@ -790,13 +767,13 @@ class BinaryWrapper {
Expected<std::pair<Constant *, Constant *>>
addSYCLOffloadEntriesToModule(StringRef EntriesFile) {
if (EntriesFile.empty() && !MySymPropReader) {
auto *NullPtr = Constant::getNullValue(getEntryPtrTy());
auto *NullPtr = Constant::getNullValue(getPtrTy());
return std::pair<Constant *, Constant *>(NullPtr, NullPtr);
}

auto *Zero = ConstantInt::get(getSizeTTy(), 0u);
auto *i32Zero = ConstantInt::get(Type::getInt32Ty(C), 0u);
auto *NullPtr = Constant::getNullValue(PointerType::getUnqual(C));
auto *NullPtr = Constant::getNullValue(getPtrTy());

std::vector<Constant *> EntriesInits;
// Only the name field is used for SYCL now, others are for future OpenMP
Expand Down Expand Up @@ -851,7 +828,7 @@ class BinaryWrapper {
switch (Prop.second.getType()) {
case llvm::util::PropertyValue::UINT32: {
// for known scalar types ValAddr is null, ValSize keeps the value
PropValAddr = Constant::getNullValue(PointerType::getUnqual(C));
PropValAddr = Constant::getNullValue(getPtrTy());
PropValSize =
ConstantInt::get(Type::getInt64Ty(C), Prop.second.asUint32());
break;
Expand Down Expand Up @@ -917,7 +894,7 @@ class BinaryWrapper {
PropRegistry = MySymPropReader->getPropRegistry();
} else {
if (PropRegistryFile.empty()) {
auto *NullPtr = Constant::getNullValue(llvm::PointerType::getUnqual(C));
auto *NullPtr = Constant::getNullValue(getPtrTy());
return std::pair<Constant *, Constant *>(NullPtr, NullPtr);
}
// load the property registry file
Expand Down Expand Up @@ -1036,12 +1013,12 @@ class BinaryWrapper {
}
} else {
// Host entry table is not used in SYCL
EntriesB = Constant::getNullValue(getEntryPtrTy());
EntriesE = Constant::getNullValue(getEntryPtrTy());
EntriesB = Constant::getNullValue(getPtrTy());
EntriesE = Constant::getNullValue(getPtrTy());
}

auto *Zero = ConstantInt::get(getSizeTTy(), 0u);
auto *NullPtr = Constant::getNullValue(PointerType::getUnqual(C));
auto *NullPtr = Constant::getNullValue(getPtrTy());
Constant *ZeroZero[] = {Zero, Zero};

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

// Get RegFuncName function declaration.
auto *RegFuncTy = FunctionType::get(
Type::getVoidTy(C),
Kind == OffloadKind::SYCL ? getSyclBinDescPtrTy() : getBinDescPtrTy(),
/*isVarArg=*/false);
auto *RegFuncTy = FunctionType::get(Type::getVoidTy(C), getPtrTy(),
/*isVarArg=*/false);
FunctionCallee RegFuncC =
M.getOrInsertFunction(Kind == OffloadKind::SYCL ? "__sycl_register_lib"
: "__tgt_register_lib",
Expand Down Expand Up @@ -1301,10 +1276,8 @@ class BinaryWrapper {
Func->setSection(".text.startup");

// Get UnregFuncName function declaration.
auto *UnRegFuncTy = FunctionType::get(
Type::getVoidTy(C),
Kind == OffloadKind::SYCL ? getSyclBinDescPtrTy() : getBinDescPtrTy(),
/*isVarArg=*/false);
auto *UnRegFuncTy = FunctionType::get(Type::getVoidTy(C), getPtrTy(),
/*isVarArg=*/false);
FunctionCallee UnRegFuncC = M.getOrInsertFunction(
Kind == OffloadKind::SYCL ? "__sycl_unregister_lib"
: "__tgt_unregister_lib",
Expand Down