Files
TheRock/compiler/amd-llvm/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

130 lines
4.9 KiB
C++

//===----------------------------------------------------------------------===//
//
// 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 file implements lowering of CIR attributes and operations directly to
// LLVMIR.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/DialectRegistry.h"
#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/GlobalVariable.h"
using namespace llvm;
namespace cir {
namespace direct {
/// Implementation of the dialect interface that converts CIR attributes to LLVM
/// IR metadata.
class CIRDialectLLVMIRTranslationInterface
: public mlir::LLVMTranslationDialectInterface {
public:
using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface;
/// Translates the given operation to LLVM IR using the provided IR builder
/// and saving the state in `moduleTranslation`.
mlir::LogicalResult convertOperation(
mlir::Operation *op, llvm::IRBuilderBase &builder,
mlir::LLVM::ModuleTranslation &moduleTranslation) const final {
if (auto cirOp = llvm::dyn_cast<mlir::LLVM::ZeroOp>(op))
moduleTranslation.mapValue(cirOp.getResult()) =
llvm::Constant::getNullValue(
moduleTranslation.convertType(cirOp.getType()));
return mlir::success();
}
/// Any named attribute in the CIR dialect, i.e, with name started with
/// "cir.", will be handled here.
virtual mlir::LogicalResult amendOperation(
mlir::Operation *op, llvm::ArrayRef<llvm::Instruction *> instructions,
mlir::NamedAttribute attribute,
mlir::LLVM::ModuleTranslation &moduleTranslation) const override {
if (auto func = dyn_cast<mlir::LLVM::LLVMFuncOp>(op)) {
if (mlir::failed(
amendFunction(func, instructions, attribute, moduleTranslation)))
return mlir::failure();
} else if (auto mod = dyn_cast<mlir::ModuleOp>(op)) {
if (mlir::failed(amendModule(mod, attribute, moduleTranslation)))
return mlir::failure();
}
return mlir::success();
}
private:
// Translate CIR function attributes to LLVM function attributes.
mlir::LogicalResult
amendFunction(mlir::LLVM::LLVMFuncOp func,
llvm::ArrayRef<llvm::Instruction *> instructions,
mlir::NamedAttribute attribute,
mlir::LLVM::ModuleTranslation &moduleTranslation) const {
llvm::Function *llvmFunc = moduleTranslation.lookupFunction(func.getName());
llvm::StringRef attrName = attribute.getName().strref();
// Strip the "cir." prefix to get the LLVM attribute name.
llvm::StringRef llvmAttrName = attrName.substr(strlen("cir."));
if (auto strAttr = mlir::dyn_cast<mlir::StringAttr>(attribute.getValue()))
llvmFunc->addFnAttr(llvmAttrName, strAttr.getValue());
return mlir::success();
}
// Translate CIR's module attributes to LLVM's module metadata
mlir::LogicalResult
amendModule(mlir::ModuleOp mod, mlir::NamedAttribute attribute,
mlir::LLVM::ModuleTranslation &moduleTranslation) const {
llvm::Module *llvmModule = moduleTranslation.getLLVMModule();
llvm::LLVMContext &llvmContext = llvmModule->getContext();
if (attribute.getName() == "cir.amdhsa_code_object_version") {
if (auto intAttr =
mlir::dyn_cast<mlir::IntegerAttr>(attribute.getValue())) {
llvmModule->addModuleFlag(llvm::Module::Error,
"amdhsa_code_object_version",
static_cast<uint32_t>(intAttr.getInt()));
}
}
if (attribute.getName() == "cir.amdgpu_printf_kind") {
if (auto strAttr =
mlir::dyn_cast<mlir::StringAttr>(attribute.getValue())) {
llvm::MDString *mdStr =
llvm::MDString::get(llvmContext, strAttr.getValue());
llvmModule->addModuleFlag(llvm::Module::Error, "amdgpu_printf_kind",
mdStr);
}
}
return mlir::success();
}
};
void registerCIRDialectTranslation(mlir::DialectRegistry &registry) {
registry.insert<cir::CIRDialect>();
registry.addExtension(+[](mlir::MLIRContext *ctx, cir::CIRDialect *dialect) {
dialect->addInterfaces<CIRDialectLLVMIRTranslationInterface>();
});
}
} // namespace direct
} // namespace cir
namespace mlir {
void registerCIRDialectTranslation(mlir::MLIRContext &context) {
mlir::DialectRegistry registry;
cir::direct::registerCIRDialectTranslation(registry);
context.appendDialectRegistry(registry);
}
} // namespace mlir