10
10
#include " clang-mlir.h"
11
11
#include " mlir/Dialect/GPU/IR/GPUDialect.h"
12
12
#include " utils.h"
13
+ #include " clang/Basic/Builtins.h"
13
14
14
15
#define DEBUG_TYPE " CGCall"
15
16
@@ -367,6 +368,8 @@ ValueCategory MLIRScanner::CallHelper(
367
368
368
369
std::pair<ValueCategory, bool >
369
370
MLIRScanner::EmitClangBuiltinCallExpr (clang::CallExpr *expr) {
371
+ auto loc = getMLIRLocation (expr->getExprLoc ());
372
+
370
373
switch (expr->getBuiltinCallee ()) {
371
374
case clang::Builtin::BImove:
372
375
case clang::Builtin::BImove_if_noexcept:
@@ -375,6 +378,34 @@ MLIRScanner::EmitClangBuiltinCallExpr(clang::CallExpr *expr) {
375
378
auto V = Visit (expr->getArg (0 ));
376
379
return make_pair (V, true );
377
380
}
381
+ case clang::Builtin::BIaddressof:
382
+ case clang::Builtin::BI__addressof:
383
+ case clang::Builtin::BI__builtin_addressof: {
384
+ auto V = Visit (expr->getArg (0 ));
385
+ assert (V.isReference );
386
+ mlir::Value val = V.val ;
387
+ auto T = getMLIRType (expr->getType ());
388
+ if (T == val.getType ())
389
+ return make_pair (ValueCategory (val, /* isRef*/ false ), true );
390
+ if (T.isa <LLVM::LLVMPointerType>()) {
391
+ if (val.getType ().isa <MemRefType>())
392
+ val = builder.create <polygeist::Memref2PointerOp>(loc, T, val);
393
+ else if (T != val.getType ())
394
+ val = builder.create <LLVM::BitcastOp>(loc, T, val);
395
+ return make_pair (ValueCategory (val, /* isRef*/ false ), true );
396
+ } else {
397
+ assert (T.isa <MemRefType>());
398
+ if (val.getType ().isa <MemRefType>())
399
+ val = builder.create <polygeist::Memref2PointerOp>(
400
+ loc, LLVM::LLVMPointerType::get (builder.getI8Type ()), val);
401
+ if (val.getType ().isa <LLVM::LLVMPointerType>())
402
+ val = builder.create <polygeist::Pointer2MemrefOp>(loc, T, val);
403
+ return make_pair (ValueCategory (val, /* isRef*/ false ), true );
404
+ }
405
+ expr->dump ();
406
+ llvm::errs () << " val: " << val << " T: " << T << " \n " ;
407
+ assert (0 && " unhandled builtin addressof" );
408
+ }
378
409
default :
379
410
break ;
380
411
}
@@ -591,36 +622,6 @@ ValueCategory MLIRScanner::VisitCallExpr(clang::CallExpr *expr) {
591
622
return Visit (expr->getArg (0 ));
592
623
}
593
624
}
594
- if (auto *ic = dyn_cast<ImplicitCastExpr>(expr->getCallee ()))
595
- if (auto *sr = dyn_cast<DeclRefExpr>(ic->getSubExpr ())) {
596
- if (sr->getDecl ()->getIdentifier () &&
597
- sr->getDecl ()->getName () == " __builtin_addressof" ) {
598
- auto V = Visit (expr->getArg (0 ));
599
- assert (V.isReference );
600
- mlir::Value val = V.val ;
601
- auto T = getMLIRType (expr->getType ());
602
- if (T == val.getType ())
603
- return ValueCategory (val, /* isRef*/ false );
604
- if (T.isa <LLVM::LLVMPointerType>()) {
605
- if (val.getType ().isa <MemRefType>())
606
- val = builder.create <polygeist::Memref2PointerOp>(loc, T, val);
607
- else if (T != val.getType ())
608
- val = builder.create <LLVM::BitcastOp>(loc, T, val);
609
- return ValueCategory (val, /* isRef*/ false );
610
- } else {
611
- assert (T.isa <MemRefType>());
612
- if (val.getType ().isa <MemRefType>())
613
- val = builder.create <polygeist::Memref2PointerOp>(
614
- loc, LLVM::LLVMPointerType::get (builder.getI8Type ()), val);
615
- if (val.getType ().isa <LLVM::LLVMPointerType>())
616
- val = builder.create <polygeist::Pointer2MemrefOp>(loc, T, val);
617
- return ValueCategory (val, /* isRef*/ false );
618
- }
619
- expr->dump ();
620
- llvm::errs () << " val: " << val << " T: " << T << " \n " ;
621
- assert (0 && " unhandled builtin addressof" );
622
- }
623
- }
624
625
if (auto *ic = dyn_cast<ImplicitCastExpr>(expr->getCallee ()))
625
626
if (auto *sr = dyn_cast<DeclRefExpr>(ic->getSubExpr ())) {
626
627
if (sr->getDecl ()->getIdentifier () &&
@@ -1406,6 +1407,11 @@ ValueCategory MLIRScanner::VisitCallExpr(clang::CallExpr *expr) {
1406
1407
}
1407
1408
#endif
1408
1409
1410
+ if (auto BI = expr->getBuiltinCallee ())
1411
+ if (!Glob.CGM .getContext ().BuiltinInfo .isPredefinedLibFunction (BI))
1412
+ llvm::errs () << " warning: we failed to emit call to builtin function "
1413
+ << Glob.CGM .getContext ().BuiltinInfo .getName (BI) << " \n " ;
1414
+
1409
1415
const auto *callee = EmitCallee (expr->getCallee ());
1410
1416
1411
1417
std::set<std::string> funcs = {
0 commit comments