Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/Value.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "llvm/Support/ErrorHandling.h"

using namespace cir;
using namespace clang;
using namespace mlir::cir;
using namespace llvm;

static RValue buildLibraryCall(CIRGenFunction &CGF, const FunctionDecl *FD,
const CallExpr *E,
mlir::Operation *calleeValue) {
auto callee = CIRGenCallee::forDirect(calleeValue, GlobalDecl(FD));
return CGF.buildCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());
}

RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
const CallExpr *E,
ReturnValueSlot ReturnValue) {
Expand Down Expand Up @@ -334,6 +342,13 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
llvm_unreachable("NYI");
break;

case Builtin::BIprintf:
if (getTarget().getTriple().isNVPTX() ||
getTarget().getTriple().isAMDGCN()) {
llvm_unreachable("NYI");
}
break;

// C++ std:: builtins.
case Builtin::BImove:
case Builtin::BImove_if_noexcept:
Expand Down Expand Up @@ -387,7 +402,8 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
// If this is a predefined lib function (e.g. malloc), emit the call
// using exactly the normal call path.
if (getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID))
llvm_unreachable("NYI");
return buildLibraryCall(*this, FD, E,
buildScalarExpr(E->getCallee()).getDefiningOp());

// Check that a call to a target specific builtin has the correct target
// features.
Expand Down
13 changes: 13 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@

#include "clang/AST/DeclCXX.h"
#include "clang/AST/GlobalDecl.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include <cassert>

#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/SymbolTable.h"
#include "mlir/IR/Types.h"

using namespace cir;
Expand Down Expand Up @@ -503,6 +506,16 @@ RValue CIRGenFunction::buildCall(const CIRGenFunctionInfo &CallInfo,
} else if (auto loadOp = dyn_cast<mlir::cir::LoadOp>(CalleePtr)) {
theCall = builder.create<mlir::cir::CallOp>(callLoc, loadOp->getResult(0),
CIRFuncTy, CIRCallArgs);
} else if (auto getGlobalOp = dyn_cast<mlir::cir::GetGlobalOp>(CalleePtr)) {
// FIXME(cir): This peephole optimization to avoids indirect calls for
// builtins. This should be fixed in the builting declaration instead by not
// emitting an unecessary get_global in the first place.
auto *globalOp = mlir::SymbolTable::lookupSymbolIn(CGM.getModule(),
getGlobalOp.getName());
assert(getGlobalOp && "undefined global function");
auto callee = llvm::dyn_cast<mlir::cir::FuncOp>(globalOp);
assert(callee && "operation is not a function");
theCall = builder.create<mlir::cir::CallOp>(callLoc, callee, CIRCallArgs);
} else {
llvm_unreachable("expected call variant to be handled");
}
Expand Down
7 changes: 7 additions & 0 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,13 @@ class CIRGetGlobalOpLowering
mlir::LogicalResult
matchAndRewrite(mlir::cir::GetGlobalOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {
// FIXME(cir): Premature DCE to avoid lowering stuff we're not using. CIRGen
// should mitigate this and not emit the get_global.
if (op->getUses().empty()) {
rewriter.eraseOp(op);
return mlir::success();
}

auto type = getTypeConverter()->convertType(op.getType());
auto symbol = op.getName();
rewriter.replaceOpWithNewOp<mlir::LLVM::AddressOfOp>(op, type, symbol);
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CIR/Executables/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %t | FileCheck %s
// REQUIRES: system-linux
// REQUIRES: target-linux
int printf(const char *format);
int printf(const char *restrict, ...);

int main (void) {
printf ("Hello, world!\n");
Expand Down