Skip to content

Commit 03678a3

Browse files
authored
[FRONTEND] make some cuda-specific functions more general; remove triton-translate (triton-lang#2811)
1 parent 73a3319 commit 03678a3

File tree

18 files changed

+193
-439
lines changed

18 files changed

+193
-439
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ if(TRITON_BUILD_PYTHON_MODULE)
221221
TritonGPUTransforms
222222
TritonNvidiaGPUTransforms
223223
TritonLLVMIR
224-
TritonPTX
225224
${dialect_libs}
226225
${conversion_libs}
227226

bin/CMakeLists.txt

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,42 +43,6 @@ target_link_libraries(triton-reduce PRIVATE
4343

4444
mlir_check_all_link_libraries(triton-reduce)
4545

46-
47-
add_llvm_executable(triton-translate triton-translate.cpp PARTIAL_SOURCES_INTENDED)
48-
llvm_update_compile_flags(triton-translate)
49-
target_link_libraries(triton-translate PRIVATE
50-
TritonAnalysis
51-
TritonTransforms
52-
TritonGPUTransforms
53-
TritonNvidiaGPUTransforms
54-
TritonLLVMIR
55-
TritonPTX
56-
${dialect_libs}
57-
${conversion_libs}
58-
# tests
59-
TritonTestAnalysis
60-
61-
LLVMCore
62-
LLVMSupport
63-
LLVMOption
64-
LLVMCodeGen
65-
LLVMAsmParser
66-
67-
# MLIR core
68-
MLIROptLib
69-
MLIRIR
70-
MLIRLLVMDialect
71-
MLIRPass
72-
MLIRSupport
73-
MLIRTransforms
74-
MLIRMathToLLVM
75-
MLIRTransformUtils
76-
MLIRLLVMToLLVMIRTranslation
77-
MLIRNVVMToLLVMIRTranslation
78-
MLIRROCDLToLLVMIRTranslation
79-
)
80-
mlir_check_all_link_libraries(triton-translate)
81-
8246
add_llvm_executable(triton-llvm-opt
8347
triton-llvm-opt.cpp
8448

bin/triton-translate.cpp

Lines changed: 0 additions & 158 deletions
This file was deleted.

include/triton/Target/LLVMIR/LLVMIRTranslation.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ std::unique_ptr<llvm::Module>
3636
translateLLVMToLLVMIR(llvm::LLVMContext *llvmContext, mlir::ModuleOp module,
3737
Target target);
3838

39+
std::string translateLLVMIRToASM(llvm::Module &module,
40+
const std::string &triple,
41+
const std::string &proc,
42+
const std::string &features,
43+
const std::vector<std::string> &flags,
44+
bool enable_fp_fusion, bool isObject);
45+
3946
bool linkExternLib(llvm::Module &module, llvm::StringRef name,
4047
llvm::StringRef path, Target target);
4148

include/triton/Target/PTX/PTXTranslation.h

Lines changed: 0 additions & 18 deletions
This file was deleted.

lib/Target/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
add_subdirectory(LLVMIR)
2-
add_subdirectory(PTX)

lib/Target/LLVMIR/LLVMIRTranslation.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,25 @@
3030
#include "llvm/Analysis/TargetTransformInfo.h"
3131
#include "llvm/IR/CallingConv.h"
3232
#include "llvm/IR/Constants.h"
33+
#include "llvm/IR/IRBuilder.h"
34+
#include "llvm/IR/LegacyPassManager.h"
3335
#include "llvm/IR/Module.h"
36+
#include "llvm/IR/Verifier.h"
3437
#include "llvm/IRReader/IRReader.h"
3538
#include "llvm/Linker/Linker.h"
39+
#include "llvm/MC/TargetRegistry.h"
40+
#include "llvm/Pass.h"
3641
#include "llvm/Passes/OptimizationLevel.h"
3742
#include "llvm/Passes/PassBuilder.h"
43+
#include "llvm/Support/CommandLine.h"
3844
#include "llvm/Support/Error.h"
3945
#include "llvm/Support/FormatVariadic.h"
4046
#include "llvm/Support/SourceMgr.h"
47+
#include "llvm/Support/TargetSelect.h"
4148
#include "llvm/Target/TargetMachine.h"
49+
#include "llvm/Transforms/IPO/AlwaysInliner.h"
4250
#include "llvm/Transforms/InstCombine/InstCombine.h"
51+
#include <filesystem>
4352
#include <optional>
4453
#ifdef _WIN32
4554
#define WIN32_LEAN_AND_MEAN
@@ -512,5 +521,81 @@ void addExternalLibs(mlir::ModuleOp &module,
512521
module.getOperation()->setAttr("triton_gpu.externs", dict);
513522
}
514523

524+
// TODO: move to python
525+
static void initLLVM() {
526+
static std::once_flag init_flag;
527+
std::call_once(init_flag, []() {
528+
LLVMInitializeNVPTXTargetInfo();
529+
LLVMInitializeNVPTXTarget();
530+
LLVMInitializeNVPTXTargetMC();
531+
LLVMInitializeNVPTXAsmPrinter();
532+
533+
LLVMInitializeAMDGPUTarget();
534+
LLVMInitializeAMDGPUTargetInfo();
535+
LLVMInitializeAMDGPUTargetMC();
536+
LLVMInitializeAMDGPUAsmParser();
537+
LLVMInitializeAMDGPUAsmPrinter();
538+
});
539+
}
540+
541+
std::string translateLLVMIRToASM(llvm::Module &module,
542+
const std::string &triple,
543+
const std::string &proc,
544+
const std::string &features,
545+
const std::vector<std::string> &flags,
546+
bool enable_fp_fusion, bool isObject) {
547+
initLLVM();
548+
// options
549+
auto options = llvm::cl::getRegisteredOptions();
550+
for (std::string flag : flags) {
551+
auto *shortPtr = static_cast<llvm::cl::opt<bool> *>(options[flag]);
552+
assert(shortPtr);
553+
shortPtr->setValue(true);
554+
}
555+
// inline everything
556+
for (llvm::Function &f : module.functions())
557+
if (!f.hasFnAttribute(llvm::Attribute::NoInline))
558+
f.addFnAttr(llvm::Attribute::AlwaysInline);
559+
// verify and store llvm
560+
llvm::legacy::PassManager pm;
561+
pm.add(llvm::createAlwaysInlinerLegacyPass());
562+
pm.add(llvm::createVerifierPass());
563+
pm.run(module);
564+
// module->print(llvm::outs(), nullptr);
565+
566+
// create machine
567+
module.setTargetTriple(triple);
568+
std::string error;
569+
auto target =
570+
llvm::TargetRegistry::lookupTarget(module.getTargetTriple(), error);
571+
llvm::TargetOptions opt;
572+
if (enable_fp_fusion)
573+
opt.AllowFPOpFusion = llvm::FPOpFusion::Fast;
574+
opt.UnsafeFPMath = false;
575+
opt.NoInfsFPMath = false;
576+
opt.NoNaNsFPMath = true;
577+
opt.TrapUnreachable = true;
578+
std::unique_ptr<llvm::TargetMachine> machine{target->createTargetMachine(
579+
module.getTargetTriple(), proc, features, opt, llvm::Reloc::PIC_,
580+
std::nullopt, llvm::CodeGenOptLevel::Aggressive)};
581+
// set data layout
582+
module.setDataLayout(machine->createDataLayout());
583+
// emit machine code
584+
std::string result;
585+
{
586+
llvm::raw_string_ostream stream(result);
587+
llvm::buffer_ostream pstream(stream);
588+
for (llvm::Function &f : module.functions())
589+
f.addFnAttr(llvm::Attribute::AlwaysInline);
590+
llvm::legacy::PassManager pass;
591+
// emit
592+
auto fileType = isObject ? llvm::CodeGenFileType::ObjectFile
593+
: llvm::CodeGenFileType::AssemblyFile;
594+
machine->addPassesToEmitFile(pass, pstream, nullptr, fileType);
595+
pass.run(module);
596+
}
597+
return result;
598+
}
599+
515600
} // namespace triton
516601
} // namespace mlir

lib/Target/PTX/CMakeLists.txt

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)