Skip to content

Commit

Permalink
[gccjit] hello, world!
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu committed Oct 24, 2024
1 parent 9d373e4 commit b3ac9af
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 48 deletions.
2 changes: 1 addition & 1 deletion include/mlir-gccjit/IR/GCCJITAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def NullAttr : GCCJIT_Attr<"Null", "null", [TypedAttrInterface]> {
def OptLevel_0 : I32EnumAttrCase<"O0", 0, "O0">;
def OptLevel_1 : I32EnumAttrCase<"O1", 1, "O1">;
def OptLevel_2 : I32EnumAttrCase<"O2", 2, "O2">;
def OptLevel_3 : I32EnumAttrCase<"O3", 3, "O2">;
def OptLevel_3 : I32EnumAttrCase<"O3", 3, "O3">;
def OptLevelEnum : I32EnumAttr<"OptLevelEnum", "Optimization level",
[OptLevel_0, OptLevel_1, OptLevel_2, OptLevel_3]> {
let cppNamespace = "mlir::gccjit";
Expand Down
4 changes: 2 additions & 2 deletions include/mlir-gccjit/IR/GCCJITOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ def CallOp : GCCJIT_Op<"call"> {
UnitAttr:$tail,
UnitAttr:$builtin
);
let results = (outs Optional<AnyType>:$result);
let results = (outs AnyType:$result);
let assemblyFormat = [{
custom<TailCallAttr>($tail)
custom<BuiltinCallAttr>($builtin)
Expand All @@ -590,7 +590,7 @@ def PtrCallOp : GCCJIT_Op<"ptr_call"> {
```
}];
let arguments = (ins GCCJIT_PointerType:$callee, Variadic<AnyType>:$args, UnitAttr:$tail);
let results = (outs Optional<AnyType>:$result);
let results = (outs AnyType:$result);
let assemblyFormat = [{
custom<TailCallAttr>($tail)
$callee `(` $args `)` `:` functional-type(operands, results) attr-dict
Expand Down
5 changes: 5 additions & 0 deletions include/mlir-gccjit/Translation/TranslateToGCCJIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ namespace mlir::gccjit {

void registerToGCCJITGimpleTranslation();
void registerToGCCJITReproducerTranslation();
void registerToGCCJITAssemblyTranslation();
void registerToGCCJITObjectTranslation();
void registerToGCCJITExecutableTranslation();
void registerToGCCJITDylibTranslation();

struct GCCJITContextDeleter {
void operator()(gcc_jit_context *ctxt) const;
Expand Down Expand Up @@ -79,6 +83,7 @@ class GCCJITTranslation {
void populateGCCJITModuleOptions();
void declareAllFunctionAndGlobals();
void translateGlobalInitializers();
void translateFunctions();
};

llvm::Expected<GCCJITContext> translateModuleToGCCJIT(ModuleOp op);
Expand Down
72 changes: 63 additions & 9 deletions src/Translation/Registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,44 @@

namespace mlir::gccjit {
namespace {
enum class OutputType {
Gimple,
Reproducer,
Assembly,
Object,
Executable,
Dylib
};

llvm::Expected<llvm::sys::fs::TempFile>
dumpContextToTempfile(gcc_jit_context *ctxt, bool reproducer) {
dumpContextToTempfile(gcc_jit_context *ctxt, OutputType type) {
auto file = llvm::sys::fs::TempFile::create("mlir-gccjit-%%%%%%%");
if (!file)
return file.takeError();
if (reproducer)
gcc_jit_context_dump_reproducer_to_file(ctxt, file->TmpName.c_str());
else
switch (type) {
case OutputType::Gimple:
gcc_jit_context_dump_to_file(ctxt, file->TmpName.c_str(), false);
break;
case OutputType::Reproducer:
gcc_jit_context_dump_reproducer_to_file(ctxt, file->TmpName.c_str());
break;
case OutputType::Assembly:
gcc_jit_context_compile_to_file(ctxt, GCC_JIT_OUTPUT_KIND_ASSEMBLER,
file->TmpName.c_str());
break;
case OutputType::Object:
gcc_jit_context_compile_to_file(ctxt, GCC_JIT_OUTPUT_KIND_OBJECT_FILE,
file->TmpName.c_str());
break;
case OutputType::Executable:
gcc_jit_context_compile_to_file(ctxt, GCC_JIT_OUTPUT_KIND_EXECUTABLE,
file->TmpName.c_str());
break;
case OutputType::Dylib:
gcc_jit_context_compile_to_file(ctxt, GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARY,
file->TmpName.c_str());
break;
}
return file;
}

Expand All @@ -49,10 +78,10 @@ LogicalResult copyFileToStream(llvm::sys::fs::TempFile file,
}

void registerTranslation(llvm::StringRef name, llvm::StringRef desc,
bool reproducer) {
OutputType type) {
TranslateFromMLIRRegistration registration(
name, desc,
[reproducer](Operation *op, raw_ostream &output) {
[type](Operation *op, raw_ostream &output) {
auto module = dyn_cast<ModuleOp>(op);
if (!module) {
op->emitError("expected 'module' operation");
Expand All @@ -63,7 +92,7 @@ void registerTranslation(llvm::StringRef name, llvm::StringRef desc,
op->emitError("failed to translate to GCCJIT context");
return failure();
}
auto file = dumpContextToTempfile(context.get().get(), reproducer);
auto file = dumpContextToTempfile(context.get().get(), type);
if (!file) {
op->emitError("failed to dump GCCJIT context to tempfile");
return failure();
Expand All @@ -79,11 +108,36 @@ void registerTranslation(llvm::StringRef name, llvm::StringRef desc,

void registerToGCCJITGimpleTranslation() {
registerTranslation("mlir-to-gccjit-gimple",
"Translate MLIR to GCCJIT's GIMPLE format", false);
"Translate MLIR to GCCJIT's GIMPLE format",
OutputType::Gimple);
}

void registerToGCCJITReproducerTranslation() {
registerTranslation("mlir-to-gccjit-reproducer",
"Translate MLIR to GCCJIT's reproducer format", true);
"Translate MLIR to GCCJIT's reproducer format",
OutputType::Reproducer);
}

void registerToGCCJITAssemblyTranslation() {
registerTranslation("mlir-to-gccjit-assembly",
"Translate MLIR to GCCJIT's assembly format",
OutputType::Assembly);
}

void registerToGCCJITObjectTranslation() {
registerTranslation("mlir-to-gccjit-object",
"Translate MLIR to GCCJIT's object file format",
OutputType::Object);
}

void registerToGCCJITExecutableTranslation() {
registerTranslation("mlir-to-gccjit-executable",
"Translate MLIR to GCCJIT's executable format",
OutputType::Executable);
}
void registerToGCCJITDylibTranslation() {
registerTranslation("mlir-to-gccjit-dylib",
"Translate MLIR to GCCJIT's dynamic library format",
OutputType::Dylib);
}
} // namespace mlir::gccjit
Loading

0 comments on commit b3ac9af

Please sign in to comment.