::llvm::cl::desc("Target architecture")};
Option<std::string> features{*this, "features",
::llvm::cl::desc("Target features")};
+ Option<int> optLevel{*this, "opt-level",
+ llvm::cl::desc("Optimization level for compilation"),
+ llvm::cl::init(2)};
Option<std::string> gpuBinaryAnnotation{
*this, "gpu-binary-annotation",
llvm::cl::desc("Annotation attribute string for GPU binary"),
void registerGpuSerializeToHsacoPass();
/// Create an instance of the GPU kernel function to CUBIN binary serialization
-/// pass.
+/// pass with optLevel (default level 2).
std::unique_ptr<Pass> createGpuSerializeToCubinPass(StringRef triple,
StringRef chip,
- StringRef features);
+ StringRef features,
+ int optLevel = 2);
/// Create an instance of the GPU kernel function to HSAco binary serialization
/// pass.
LINK_COMPONENTS
Core
MC
+ Target
${NVPTX_LIBS}
${AMDGPU_LIBS}
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/GPU/Transforms/Passes.h"
+#include "mlir/ExecutionEngine/OptUtils.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
LogicalResult
gpu::SerializeToBlobPass::optimizeLlvm(llvm::Module &llvmModule,
llvm::TargetMachine &targetMachine) {
- // TODO: If serializeToCubin ends up defining optimizations, factor them
- // into here from SerializeToHsaco
+ int optLevel = this->optLevel.getValue();
+ if (optLevel < 0 || optLevel > 3)
+ return getOperation().emitError()
+ << "invalid optimization level " << optLevel;
+
+ targetMachine.setOptLevel(static_cast<llvm::CodeGenOpt::Level>(optLevel));
+
+ auto transformer =
+ makeOptimizingTransformer(optLevel, /*sizeLevel=*/0, &targetMachine);
+ auto error = transformer(&llvmModule);
+ if (error) {
+ InFlightDiagnostic mlirError = getOperation()->emitError();
+ llvm::handleAllErrors(
+ std::move(error), [&mlirError](const llvm::ErrorInfoBase &ei) {
+ mlirError << "could not optimize LLVM IR: " << ei.message();
+ });
+ return mlirError;
+ }
return success();
}
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(SerializeToCubinPass)
SerializeToCubinPass(StringRef triple = "nvptx64-nvidia-cuda",
- StringRef chip = "sm_35", StringRef features = "+ptx60");
+ StringRef chip = "sm_35", StringRef features = "+ptx60",
+ int optLevel = 2);
StringRef getArgument() const override { return "gpu-to-cubin"; }
StringRef getDescription() const override {
}
SerializeToCubinPass::SerializeToCubinPass(StringRef triple, StringRef chip,
- StringRef features) {
+ StringRef features, int optLevel) {
maybeSetOption(this->triple, triple);
maybeSetOption(this->chip, chip);
maybeSetOption(this->features, features);
+ if (this->optLevel.getNumOccurrences() == 0)
+ this->optLevel.setValue(optLevel);
}
void SerializeToCubinPass::getDependentDialects(
std::unique_ptr<Pass> mlir::createGpuSerializeToCubinPass(StringRef triple,
StringRef arch,
- StringRef features) {
- return std::make_unique<SerializeToCubinPass>(triple, arch, features);
+ StringRef features,
+ int optLevel) {
+ return std::make_unique<SerializeToCubinPass>(triple, arch, features,
+ optLevel);
}
#else // MLIR_GPU_TO_CUBIN_PASS_ENABLE
}
protected:
- Option<int> optLevel{
- *this, "opt-level",
- llvm::cl::desc("Optimization level for HSACO compilation"),
- llvm::cl::init(2)};
-
Option<std::string> rocmPath{*this, "rocm-path",
llvm::cl::desc("Path to ROCm install")};
std::unique_ptr<llvm::Module>
translateToLLVMIR(llvm::LLVMContext &llvmContext) override;
- /// Adds LLVM optimization passes
- LogicalResult optimizeLlvm(llvm::Module &llvmModule,
- llvm::TargetMachine &targetMachine) override;
-
private:
void getDependentDialects(DialectRegistry ®istry) const override;
return ret;
}
-LogicalResult
-SerializeToHsacoPass::optimizeLlvm(llvm::Module &llvmModule,
- llvm::TargetMachine &targetMachine) {
- int optLevel = this->optLevel.getValue();
- if (optLevel < 0 || optLevel > 3)
- return getOperation().emitError()
- << "Invalid HSA optimization level" << optLevel << "\n";
-
- targetMachine.setOptLevel(static_cast<llvm::CodeGenOpt::Level>(optLevel));
-
- auto transformer =
- makeOptimizingTransformer(optLevel, /*sizeLevel=*/0, &targetMachine);
- auto error = transformer(&llvmModule);
- if (error) {
- InFlightDiagnostic mlirError = getOperation()->emitError();
- llvm::handleAllErrors(
- std::move(error), [&mlirError](const llvm::ErrorInfoBase &ei) {
- mlirError << "Could not optimize LLVM IR: " << ei.message() << "\n";
- });
- return mlirError;
- }
- return success();
-}
-
std::unique_ptr<SmallVectorImpl<char>>
SerializeToHsacoPass::assembleIsa(const std::string &isa) {
auto loc = getOperation().getLoc();