Skip to content

Commit

Permalink
[gccjit] support more function related translation
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu committed Oct 22, 2024
1 parent d3b06a5 commit 9cbac0b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/mlir-gccjit/IR/GCCJITOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ def FuncOp : GCCJIT_Op<"func", [IsolatedFromAbove]> {

/// Checks if the function is imported.
bool isImported() { return getFnKind() == FnKind::Imported; }

/// Get aliasee if the function is an alias.
FlatSymbolRefAttr getAliasee();
}];
}

Expand Down
12 changes: 12 additions & 0 deletions src/GCCJITOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ LogicalResult gccjit::FuncOp::verify() {
return success();
}

FlatSymbolRefAttr FuncOp::getAliasee() {
FlatSymbolRefAttr res{};
for (auto attr : getGccjitFnAttrs()) {
auto fnAttr = cast<FunctionAttr>(attr);
if (fnAttr.getAttr().getValue() == FnAttrEnum::Alias) {
res = FlatSymbolRefAttr::get(getContext(), fnAttr.getStrValue());
break;
}
}
return res;
}

//===----------------------------------------------------------------------===//
// ReturnOp
//===----------------------------------------------------------------------===//
Expand Down
27 changes: 26 additions & 1 deletion src/Translation/TranslateToGCCJIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "mlir/IR/Block.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Location.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/SymbolTable.h"
#include "mlir/IR/TypeRange.h"
Expand Down Expand Up @@ -62,6 +63,7 @@ struct Translator {
void declareAllFunctionAndGlobals();
static gcc_jit_function_kind convertFnKind(FnKind kind);
MLIRContext *getMLIRContext();
gcc_jit_location *getLocation(LocationAttr loc);
};

} // namespace impl
Expand Down Expand Up @@ -162,6 +164,10 @@ void Translator::populateGCCJITModuleOptions() {
if (auto boolAttr = dyn_cast<BoolAttr>(attr.getValue()))
gcc_jit_context_set_bool_allow_unreachable_blocks(ctxt,
boolAttr.getValue());
} else if (attr.getName() == "gccjit.debug_info") {
if (auto boolAttr = dyn_cast<BoolAttr>(attr.getValue()))
gcc_jit_context_set_bool_option(ctxt, GCC_JIT_BOOL_OPTION_DEBUGINFO,
boolAttr.getValue());
}
}
}
Expand Down Expand Up @@ -199,7 +205,7 @@ void Translator::declareAllFunctionAndGlobals() {
ctxt, /*todo: location*/ nullptr, type, name.c_str());
});
auto *funcHandle = gcc_jit_context_new_function(
ctxt, /*todo: location*/ nullptr, kind, returnType, name.c_str(),
ctxt, getLocation(func.getLoc()), kind, returnType, name.c_str(),
paramTypes.size(), params.data(), type.isVarArg());
SymbolRefAttr symRef = SymbolRefAttr::get(getMLIRContext(), name);
functionMap[symRef] = {funcHandle, std::move(params)};
Expand All @@ -208,6 +214,25 @@ void Translator::declareAllFunctionAndGlobals() {

MLIRContext *Translator::getMLIRContext() { return moduleOp.getContext(); }

gcc_jit_location *Translator::getLocation(LocationAttr loc) {
if (!loc)
return nullptr;
return llvm::TypeSwitch<LocationAttr, gcc_jit_location *>(loc)
.Case([&](FileLineColLoc loc) {
return gcc_jit_context_new_location(ctxt,
loc.getFilename().str().c_str(),
loc.getLine(), loc.getColumn());
})
.Case([&](CallSiteLoc loc) { return getLocation(loc.getCaller()); })
.Case(
[&](FusedLoc loc) { return getLocation(loc.getLocations().front()); })
.Case([&](NameLoc loc) { return getLocation(loc.getChildLoc()); })
.Case(
[&](OpaqueLoc loc) { return getLocation(loc.getFallbackLocation()); })
.Case([&](UnknownLoc loc) { return nullptr; })
.Default([](LocationAttr) { return nullptr; });
}

} // namespace impl

GCCJITContext Translator::takeContext() {
Expand Down

0 comments on commit 9cbac0b

Please sign in to comment.