Skip to content

Commit 185d4f4

Browse files
[CIR] builtin operator new/delete
1 parent ea66d26 commit 185d4f4

File tree

5 files changed

+40
-0
lines changed

5 files changed

+40
-0
lines changed

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ struct MissingFeatures {
200200
static bool aggValueSlotMayOverlap() { return false; }
201201
static bool aggValueSlotVolatile() { return false; }
202202
static bool alignCXXRecordDecl() { return false; }
203+
static bool allocToken() { return false; }
203204
static bool appleKext() { return false; }
204205
static bool armComputeVolatileBitfields() { return false; }
205206
static bool asmGoto() { return false; }

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "mlir/IR/BuiltinAttributes.h"
1919
#include "mlir/IR/Value.h"
2020
#include "mlir/Support/LLVM.h"
21+
#include "clang/AST/DeclBase.h"
2122
#include "clang/AST/Expr.h"
2223
#include "clang/AST/GlobalDecl.h"
2324
#include "clang/Basic/Builtins.h"
@@ -520,6 +521,13 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
520521
cir::PrefetchOp::create(builder, loc, address, locality, isWrite);
521522
return RValue::get(nullptr);
522523
}
524+
case Builtin::BI__builtin_operator_new:
525+
return emitNewOrDeleteBuiltinCall(
526+
e->getCallee()->getType()->castAs<FunctionProtoType>(), e, false);
527+
case Builtin::BI__builtin_operator_delete:
528+
emitNewOrDeleteBuiltinCall(
529+
e->getCallee()->getType()->castAs<FunctionProtoType>(), e, true);
530+
return RValue::get(nullptr);
523531
}
524532

525533
// If this is an alias for a lib function (e.g. __builtin_sin), emit
@@ -559,6 +567,7 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
559567
std::string("unimplemented builtin call: ") +
560568
getContext().BuiltinInfo.getName(builtinID));
561569
return getUndefRValue(e->getType());
570+
562571
}
563572

564573
static mlir::Value emitTargetArchBuiltinExpr(CIRGenFunction *cgf,

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,6 +2006,7 @@ RValue CIRGenFunction::emitCallExpr(const clang::CallExpr *e,
20062006
return emitCall(e->getCallee()->getType(), callee, e, returnValue);
20072007
}
20082008

2009+
20092010
/// Emit code to compute the specified expression, ignoring the result.
20102011
void CIRGenFunction::emitIgnoredExpr(const Expr *e) {
20112012
if (e->isPRValue()) {

clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,32 @@ static RValue emitNewDeleteCall(CIRGenFunction &cgf,
610610
return rv;
611611
}
612612

613+
RValue CIRGenFunction::emitNewOrDeleteBuiltinCall(const FunctionProtoType *type,
614+
const CallExpr *callExpr,
615+
bool isDelete) {
616+
CallArgList args;
617+
emitCallArgs(args, type, callExpr->arguments());
618+
// Find the allocation or deallocation function that we're calling.
619+
ASTContext &astContext = getContext();
620+
DeclarationName name = astContext.DeclarationNames.getCXXOperatorName(
621+
isDelete ? OO_Delete : OO_New);
622+
623+
clang::DeclContextLookupResult lookupResult = astContext.getTranslationUnitDecl()->lookup(name);
624+
for (const auto *decl : lookupResult) {
625+
if (const auto *funcDecl = dyn_cast<FunctionDecl>(decl)) {
626+
if (astContext.hasSameType(funcDecl->getType(), QualType(type, 0))) {
627+
// Used for -fsanitize=alloc-token
628+
assert(!cir::MissingFeatures::allocToken());
629+
630+
// Emit the call to operator new/delete.
631+
return emitNewDeleteCall(*this, funcDecl, type, args);
632+
}
633+
}
634+
}
635+
636+
llvm_unreachable("predeclared global operator new/delete is missing");
637+
}
638+
613639
namespace {
614640
/// Calls the given 'operator delete' on a single object.
615641
struct CallObjectDelete final : EHScopeStack::Cleanup {

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,9 @@ class CIRGenFunction : public CIRGenTypeCache {
14761476

14771477
RValue emitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *expr);
14781478

1479+
RValue emitNewOrDeleteBuiltinCall(const FunctionProtoType* type,
1480+
const CallExpr* call, bool isDelete);
1481+
14791482
void emitCXXTemporary(const CXXTemporary *temporary, QualType tempType,
14801483
Address ptr);
14811484

0 commit comments

Comments
 (0)