20
20
#include " mlir/Support/LLVM.h"
21
21
#include " clang/AST/Expr.h"
22
22
#include " clang/AST/GlobalDecl.h"
23
+ #include " clang/CIR/MissingFeatures.h"
23
24
#include " llvm/Support/ErrorHandling.h"
24
25
25
26
using namespace clang ;
26
27
using namespace clang ::CIRGen;
28
+ using namespace llvm ;
29
+
30
+ static RValue emitLibraryCall (CIRGenFunction &cgf, const FunctionDecl *fd,
31
+ const CallExpr *e, mlir::Operation *calleeValue) {
32
+ CIRGenCallee callee = CIRGenCallee::forDirect (calleeValue, GlobalDecl (fd));
33
+ return cgf.emitCall (e->getCallee ()->getType (), callee, e, ReturnValueSlot ());
34
+ }
27
35
28
36
RValue CIRGenFunction::emitBuiltinExpr (const GlobalDecl &gd, unsigned builtinID,
29
37
const CallExpr *e,
@@ -49,7 +57,34 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
49
57
}
50
58
}
51
59
52
- mlir::Location loc = getLoc (e->getExprLoc ());
53
- cgm.errorNYI (loc, " non constant foldable builtin calls" );
60
+ const FunctionDecl *fd = gd.getDecl ()->getAsFunction ();
61
+
62
+ // If this is an alias for a lib function (e.g. __builtin_sin), emit
63
+ // the call using the normal call path, but using the unmangled
64
+ // version of the function name.
65
+ if (getContext ().BuiltinInfo .isLibFunction (builtinID))
66
+ return emitLibraryCall (*this , fd, e,
67
+ cgm.getBuiltinLibFunction (fd, builtinID));
68
+
69
+ cgm.errorNYI (e->getSourceRange (), " non constant foldable builtin calls" );
54
70
return getUndefRValue (e->getType ());
55
71
}
72
+
73
+ // / Given a builtin id for a function like "__builtin_fabsf", return a Function*
74
+ // / for "fabsf".
75
+ cir::FuncOp CIRGenModule::getBuiltinLibFunction (const FunctionDecl *fd,
76
+ unsigned builtinID) {
77
+ assert (astContext.BuiltinInfo .isLibFunction (builtinID));
78
+
79
+ // Get the name, skip over the __builtin_ prefix (if necessary). We may have
80
+ // to build this up so provide a small stack buffer to handle the vast
81
+ // majority of names.
82
+ llvm::SmallString<64 > name;
83
+
84
+ assert (!cir::MissingFeatures::asmLabelAttr ());
85
+ name = astContext.BuiltinInfo .getName (builtinID).substr (10 );
86
+
87
+ GlobalDecl d (fd);
88
+ mlir::Type type = convertType (fd->getType ());
89
+ return getOrCreateCIRFunction (name, type, d, /* forVTable=*/ false );
90
+ }
0 commit comments