Skip to content

Commit

Permalink
[FPGA][SYCL] Allow memory attributes applied on struct members of dev…
Browse files Browse the repository at this point in the history
…ice_globals (intel#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>
  • Loading branch information
smanna12 committed Jul 15, 2024
1 parent 8761d40 commit 5f418ab
Show file tree
Hide file tree
Showing 2 changed files with 404 additions and 0 deletions.
40 changes: 40 additions & 0 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5907,10 +5907,50 @@ void CodeGenModule::generateIntelFPGAAnnotation(
}
}

/**
* Adds global Intel FPGA annotations for a given variable declaration.
* This function handles both simple global variables and fields within
* structs that are annotated with Intel FPGA attributes. For structs,
* it recursively visits all fields and base classes to collect annotations.
*
* @param VD The variable declaration to annotate.
* @param GV The LLVM GlobalValue corresponding to the variable declaration.
*/
void CodeGenModule::addGlobalIntelFPGAAnnotation(const VarDecl *VD,
llvm::GlobalValue *GV) {
SmallString<256> AnnotStr;

// Handle annotations for fields within a device_global struct.
if (getLangOpts().IntelFPGA && VD->getType()->isRecordType()) {
auto RT = VD->getType()->getAs<RecordType>();

auto Gen = [&AnnotStr, this](const RecordType *Ty, auto &&Gen) -> void {
const CXXRecordDecl *RD = cast<CXXRecordDecl>(Ty->getDecl());

// Iterate over the fields of the struct.
for (const auto *Field : RD->fields()) {
generateIntelFPGAAnnotation(Field, AnnotStr);

if (const auto *FT =
Field->getType()
->getPointeeOrArrayElementType() // Strip pointers/arrays
->getAs<RecordType>())
Gen(FT, Gen);
}

// Iterate over the base classes of the struct.
for (const auto Base : RD->bases()) {
QualType BaseTy = Base.getType();

if (const auto *BRT = BaseTy->getAs<RecordType>())
Gen(BRT, Gen);
}
};
Gen(RT, Gen);
}

generateIntelFPGAAnnotation(VD, AnnotStr);

if (!AnnotStr.empty()) {
// Get the globals for file name, annotation, and the line number.
llvm::Constant *AnnoGV = EmitAnnotationString(AnnotStr),
Expand Down
Loading

0 comments on commit 5f418ab

Please sign in to comment.