llvm-project/clang/lib/CIR/CodeGen/CIRGenAMDGPU.cpp
Chaitanya a4f97f0d90
[CIR][AMDGPU] Add module flags for AMDGPU target using amendOperation of CIRDialectLLVMIRTranslationInterface (#186073)
Add the amendOperation override to handle CIR dialect attributes during
MLIR-to-LLVM IR translation. This dispatches to  amendModule for ModuleOp,
enabling module metadata.

This PR also adds support to emit AMDGPU-specific module flags
amdhsa_code_object_version and amdgpu_printf_kind to match OGCG
behavior.

In CIRGenModule, the flags are stored as CIR module attributes:

cir.amdhsa_code_object_version (integer)
cir.amdgpu_printf_kind (string: "hostcall" or "buffered")
During lowering to LLVM IR (in LowerToLLVMIR.cpp), these attributes are
converted to LLVM module flags.

Upstreaming basic changes from clangIR PRs: 

61e9ebd9f8
https://github.com/llvm/clangir/pull/768
https://github.com/llvm/clangir/pull/773
https://github.com/llvm/clangir/pull/2100
2026-03-20 12:20:42 +05:30

42 lines
1.5 KiB
C++

//===- CIRGenAMDGPU.cpp - AMDGPU-specific logic for CIR generation --------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This contains code dealing with AMDGPU-specific logic of CIR generation.
//
//===----------------------------------------------------------------------===//
#include "CIRGenModule.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "llvm/TargetParser/Triple.h"
using namespace clang;
using namespace clang::CIRGen;
void CIRGenModule::emitAMDGPUMetadata() {
// Emit code object version module flag.
if (target.getTargetOpts().CodeObjectVersion !=
llvm::CodeObjectVersionKind::COV_None) {
theModule->setAttr(
cir::CIRDialect::getAMDGPUCodeObjectVersionAttrName(),
builder.getI32IntegerAttr(target.getTargetOpts().CodeObjectVersion));
}
// Emit printf kind module flag for HIP.
if (langOpts.HIP) {
llvm::StringRef printfKind =
target.getTargetOpts().AMDGPUPrintfKindVal ==
TargetOptions::AMDGPUPrintfKind::Hostcall
? "hostcall"
: "buffered";
theModule->setAttr(cir::CIRDialect::getAMDGPUPrintfKindAttrName(),
builder.getStringAttr(printfKind));
}
}