Skip to content

Commit 5f418ab

Browse files
authored
[FPGA][SYCL] Allow memory attributes applied on struct members of device_globals (#14414)
This commit addresses an issue where FPGA memory attributes applied to struct member variables were not being propagated as global annotations (@llvm.global.annotation) when a device_global is templated on that struct. As a result, these attributes were silently dropped, with no warning or error provided to the user. The fix ensures that when structs with memory attributes are used to instantiate a device_global, the corresponding global annotations are correctly added to the LLVM IR. This allows the FPGA backend to properly recognize and utilize these attributes, aligning the behavior with that of internal, kernel-scoped memory, where ptr.annotations are correctly added. Key changes include: - Enhancing the addGlobalIntelFPGAAnnotation function to recursively process struct fields and base classes, ensuring all relevant attributes are captured and annotated in the global scope. - Ensuring that annotations are not only applied to simple global variables but also to fields within structs that are used in device_global templates. This fix improves the robustness of the code generation process and ensures that fpga memory attributes on structs are effectively communicated to the backend. Patch by: Mariya Podchishchaeva(mariya.podchishchaeva@intel.com) and Soumi Manna (soumi.manna@intel.com) --------- Signed-off-by: Soumi Manna <soumi.manna@intel.com>
1 parent 8761d40 commit 5f418ab

File tree

2 files changed

+404
-0
lines changed

2 files changed

+404
-0
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5907,10 +5907,50 @@ void CodeGenModule::generateIntelFPGAAnnotation(
59075907
}
59085908
}
59095909

5910+
/**
5911+
* Adds global Intel FPGA annotations for a given variable declaration.
5912+
* This function handles both simple global variables and fields within
5913+
* structs that are annotated with Intel FPGA attributes. For structs,
5914+
* it recursively visits all fields and base classes to collect annotations.
5915+
*
5916+
* @param VD The variable declaration to annotate.
5917+
* @param GV The LLVM GlobalValue corresponding to the variable declaration.
5918+
*/
59105919
void CodeGenModule::addGlobalIntelFPGAAnnotation(const VarDecl *VD,
59115920
llvm::GlobalValue *GV) {
59125921
SmallString<256> AnnotStr;
5922+
5923+
// Handle annotations for fields within a device_global struct.
5924+
if (getLangOpts().IntelFPGA && VD->getType()->isRecordType()) {
5925+
auto RT = VD->getType()->getAs<RecordType>();
5926+
5927+
auto Gen = [&AnnotStr, this](const RecordType *Ty, auto &&Gen) -> void {
5928+
const CXXRecordDecl *RD = cast<CXXRecordDecl>(Ty->getDecl());
5929+
5930+
// Iterate over the fields of the struct.
5931+
for (const auto *Field : RD->fields()) {
5932+
generateIntelFPGAAnnotation(Field, AnnotStr);
5933+
5934+
if (const auto *FT =
5935+
Field->getType()
5936+
->getPointeeOrArrayElementType() // Strip pointers/arrays
5937+
->getAs<RecordType>())
5938+
Gen(FT, Gen);
5939+
}
5940+
5941+
// Iterate over the base classes of the struct.
5942+
for (const auto Base : RD->bases()) {
5943+
QualType BaseTy = Base.getType();
5944+
5945+
if (const auto *BRT = BaseTy->getAs<RecordType>())
5946+
Gen(BRT, Gen);
5947+
}
5948+
};
5949+
Gen(RT, Gen);
5950+
}
5951+
59135952
generateIntelFPGAAnnotation(VD, AnnotStr);
5953+
59145954
if (!AnnotStr.empty()) {
59155955
// Get the globals for file name, annotation, and the line number.
59165956
llvm::Constant *AnnoGV = EmitAnnotationString(AnnotStr),

0 commit comments

Comments
 (0)