Skip to content

Commit

Permalink
[CIR][ABI][NFC] Make createLowerModule public (#734)
Browse files Browse the repository at this point in the history
Although currently LowerModule is not ready for formal usage, we need it
for target-specific lowering to LLVM. This PR temporarily public the
symbol `createLowerModule` to reuse the logic of preparing a
`LowerModule`, making it easier for future refactor (making
`TargetLoweringInfo` available for most stages in CIR Lowering).
  • Loading branch information
seven-mile authored and lanza committed Nov 3, 2024
1 parent 040c0e2 commit 62c7af6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
36 changes: 2 additions & 34 deletions clang/lib/CIR/Dialect/Transforms/CallConvLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@
//
//===----------------------------------------------------------------------===//

// FIXME(cir): This header file is not exposed to the public API, but can be
// reused by CIR ABI lowering since it holds target-specific information.
#include "../../../Basic/Targets.h"

#include "TargetLowering/LowerModule.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"

#define GEN_PASS_DEF_CALLCONVLOWERING
Expand All @@ -25,35 +21,6 @@
namespace mlir {
namespace cir {

namespace {

LowerModule createLowerModule(FuncOp op, PatternRewriter &rewriter) {
auto module = op->getParentOfType<mlir::ModuleOp>();

// Fetch the LLVM data layout string.
auto dataLayoutStr = cast<StringAttr>(
module->getAttr(LLVM::LLVMDialect::getDataLayoutAttrName()));

// Fetch target information.
llvm::Triple triple(
cast<StringAttr>(module->getAttr("cir.triple")).getValue());
clang::TargetOptions targetOptions;
targetOptions.Triple = triple.str();
auto targetInfo = clang::targets::AllocateTarget(triple, targetOptions);

// FIXME(cir): This just uses the default language options. We need to account
// for custom options.
// Create context.
assert(!::cir::MissingFeatures::langOpts());
clang::LangOptions langOpts;
auto context = CIRLowerContext(module, langOpts);
context.initBuiltinTypes(*targetInfo);

return LowerModule(context, module, dataLayoutStr, *targetInfo, rewriter);
}

} // namespace

//===----------------------------------------------------------------------===//
// Rewrite Patterns
//===----------------------------------------------------------------------===//
Expand All @@ -68,7 +35,8 @@ struct CallConvLoweringPattern : public OpRewritePattern<FuncOp> {
if (!op.getAst())
return op.emitError("function has no AST information");

LowerModule lowerModule = createLowerModule(op, rewriter);
auto modOp = op->getParentOfType<ModuleOp>();
LowerModule lowerModule = createLowerModule(modOp, rewriter);

// Rewrite function calls before definitions. This should be done before
// lowering the definition.
Expand Down
32 changes: 31 additions & 1 deletion clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
//
//===----------------------------------------------------------------------===//

#include "LowerModule.h"
// FIXME(cir): This header file is not exposed to the public API, but can be
// reused by CIR ABI lowering since it holds target-specific information.
#include "../../../../Basic/Targets.h"
#include "clang/Basic/TargetOptions.h"

#include "CIRLowerContext.h"
#include "LowerFunction.h"
#include "LowerModule.h"
#include "TargetInfo.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/PatternMatch.h"
Expand Down Expand Up @@ -208,5 +214,29 @@ LogicalResult LowerModule::rewriteFunctionCall(CallOp callOp, FuncOp funcOp) {
return success();
}

// TODO: not to create it every time
LowerModule createLowerModule(ModuleOp module, PatternRewriter &rewriter) {
// Fetch the LLVM data layout string.
auto dataLayoutStr = cast<StringAttr>(
module->getAttr(LLVM::LLVMDialect::getDataLayoutAttrName()));

// Fetch target information.
llvm::Triple triple(
cast<StringAttr>(module->getAttr("cir.triple")).getValue());
clang::TargetOptions targetOptions;
targetOptions.Triple = triple.str();
auto targetInfo = clang::targets::AllocateTarget(triple, targetOptions);

// FIXME(cir): This just uses the default language options. We need to account
// for custom options.
// Create context.
assert(!::cir::MissingFeatures::langOpts());
clang::LangOptions langOpts;
auto context = CIRLowerContext(module, langOpts);
context.initBuiltinTypes(*targetInfo);

return LowerModule(context, module, dataLayoutStr, *targetInfo, rewriter);
}

} // namespace cir
} // namespace mlir
2 changes: 2 additions & 0 deletions clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class LowerModule {
LogicalResult rewriteFunctionCall(CallOp callOp, FuncOp funcOp);
};

LowerModule createLowerModule(ModuleOp module, PatternRewriter &rewriter);

} // namespace cir
} // namespace mlir

Expand Down

0 comments on commit 62c7af6

Please sign in to comment.