-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathCIRGenFunction.cpp
1901 lines (1600 loc) · 68.3 KB
/
CIRGenFunction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===- CIRGenFunction.cpp - Emit CIR from ASTs for a Function -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This coordinates the per-function state used while generating code
//
//===----------------------------------------------------------------------===//
#include "CIRGenFunction.h"
#include "CIRGenCXXABI.h"
#include "CIRGenModule.h"
#include "CIRGenOpenMPRuntime.h"
#include "clang/AST/Attrs.inc"
#include "clang/Basic/CodeGenOptions.h"
#include "clang/CIR/MissingFeatures.h"
#include "clang/AST/ASTLambda.h"
#include "clang/AST/ExprObjC.h"
#include "clang/Basic/Builtins.h"
#include "clang/Basic/DiagnosticCategories.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/IR/FPEnv.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "llvm/ADT/PointerIntPair.h"
#include "CIRGenTBAA.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Support/LogicalResult.h"
using namespace clang;
using namespace clang::CIRGen;
using namespace cir;
CIRGenFunction::CIRGenFunction(CIRGenModule &CGM, CIRGenBuilderTy &builder,
bool suppressNewContext)
: CIRGenTypeCache(CGM), CGM{CGM}, builder(builder),
SanOpts(CGM.getLangOpts().Sanitize), CurFPFeatures(CGM.getLangOpts()),
ShouldEmitLifetimeMarkers(false) {
if (!suppressNewContext)
CGM.getCXXABI().getMangleContext().startNewFunction();
EHStack.setCGF(this);
// TODO(CIR): SetFastMathFlags(CurFPFeatures);
}
CIRGenFunction::~CIRGenFunction() {
assert(LifetimeExtendedCleanupStack.empty() && "failed to emit a cleanup");
assert(DeferredDeactivationCleanupStack.empty() &&
"missed to deactivate a cleanup");
// TODO(cir): set function is finished.
assert(!cir::MissingFeatures::openMPRuntime());
// If we have an OpenMPIRBuilder we want to finalize functions (incl.
// outlining etc) at some point. Doing it once the function codegen is done
// seems to be a reasonable spot. We do it here, as opposed to the deletion
// time of the CodeGenModule, because we have to ensure the IR has not yet
// been "emitted" to the outside, thus, modifications are still sensible.
assert(!cir::MissingFeatures::openMPRuntime());
}
clang::ASTContext &CIRGenFunction::getContext() const {
return CGM.getASTContext();
}
mlir::Type CIRGenFunction::ConvertType(QualType T) {
return CGM.getTypes().ConvertType(T);
}
cir::TypeEvaluationKind CIRGenFunction::getEvaluationKind(QualType type) {
type = type.getCanonicalType();
while (true) {
switch (type->getTypeClass()) {
#define TYPE(name, parent)
#define ABSTRACT_TYPE(name, parent)
#define NON_CANONICAL_TYPE(name, parent) case Type::name:
#define DEPENDENT_TYPE(name, parent) case Type::name:
#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(name, parent) case Type::name:
#include "clang/AST/TypeNodes.inc"
llvm_unreachable("non-canonical or dependent type in IR-generation");
case Type::ArrayParameter:
case Type::HLSLAttributedResource:
llvm_unreachable("NYI");
case Type::Auto:
case Type::DeducedTemplateSpecialization:
llvm_unreachable("undeduced type in IR-generation");
// Various scalar types.
case Type::Builtin:
case Type::Pointer:
case Type::BlockPointer:
case Type::LValueReference:
case Type::RValueReference:
case Type::MemberPointer:
case Type::Vector:
case Type::ExtVector:
case Type::ConstantMatrix:
case Type::FunctionProto:
case Type::FunctionNoProto:
case Type::Enum:
case Type::ObjCObjectPointer:
case Type::Pipe:
case Type::BitInt:
return cir::TEK_Scalar;
// Complexes.
case Type::Complex:
return cir::TEK_Complex;
// Arrays, records, and Objective-C objects.
case Type::ConstantArray:
case Type::IncompleteArray:
case Type::VariableArray:
case Type::Record:
case Type::ObjCObject:
case Type::ObjCInterface:
return cir::TEK_Aggregate;
// We operate on atomic values according to their underlying type.
case Type::Atomic:
type = cast<AtomicType>(type)->getValueType();
continue;
}
llvm_unreachable("unknown type kind!");
}
}
mlir::Type CIRGenFunction::convertTypeForMem(QualType T) {
return CGM.getTypes().convertTypeForMem(T);
}
mlir::Type CIRGenFunction::convertType(QualType T) {
return CGM.getTypes().ConvertType(T);
}
mlir::Location CIRGenFunction::getLoc(SourceLocation SLoc) {
// Some AST nodes might contain invalid source locations (e.g.
// CXXDefaultArgExpr), workaround that to still get something out.
if (SLoc.isValid()) {
const SourceManager &SM = getContext().getSourceManager();
PresumedLoc PLoc = SM.getPresumedLoc(SLoc);
StringRef Filename = PLoc.getFilename();
return mlir::FileLineColLoc::get(builder.getStringAttr(Filename),
PLoc.getLine(), PLoc.getColumn());
} else {
// Do our best...
assert(currSrcLoc && "expected to inherit some source location");
return *currSrcLoc;
}
}
mlir::Location CIRGenFunction::getLoc(SourceRange SLoc) {
// Some AST nodes might contain invalid source locations (e.g.
// CXXDefaultArgExpr), workaround that to still get something out.
if (SLoc.isValid()) {
mlir::Location B = getLoc(SLoc.getBegin());
mlir::Location E = getLoc(SLoc.getEnd());
SmallVector<mlir::Location, 2> locs = {B, E};
mlir::Attribute metadata;
return mlir::FusedLoc::get(locs, metadata, &getMLIRContext());
} else if (currSrcLoc) {
return *currSrcLoc;
}
// We're brave, but time to give up.
return builder.getUnknownLoc();
}
mlir::Location CIRGenFunction::getLoc(mlir::Location lhs, mlir::Location rhs) {
SmallVector<mlir::Location, 2> locs = {lhs, rhs};
mlir::Attribute metadata;
return mlir::FusedLoc::get(locs, metadata, &getMLIRContext());
}
/// Return true if the statement contains a label in it. If
/// this statement is not executed normally, it not containing a label means
/// that we can just remove the code.
bool CIRGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
// Null statement, not a label!
if (!S)
return false;
// If this is a label, we have to emit the code, consider something like:
// if (0) { ... foo: bar(); } goto foo;
//
// TODO: If anyone cared, we could track __label__'s, since we know that you
// can't jump to one from outside their declared region.
if (isa<LabelStmt>(S))
return true;
// If this is a case/default statement, and we haven't seen a switch, we
// have to emit the code.
if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
return true;
// If this is a switch statement, we want to ignore cases below it.
if (isa<SwitchStmt>(S))
IgnoreCaseStmts = true;
// Scan subexpressions for verboten labels.
for (const Stmt *SubStmt : S->children())
if (ContainsLabel(SubStmt, IgnoreCaseStmts))
return true;
return false;
}
bool CIRGenFunction::sanitizePerformTypeCheck() const {
return SanOpts.has(SanitizerKind::Null) ||
SanOpts.has(SanitizerKind::Alignment) ||
SanOpts.has(SanitizerKind::ObjectSize) ||
SanOpts.has(SanitizerKind::Vptr);
}
void CIRGenFunction::emitTypeCheck(TypeCheckKind TCK, clang::SourceLocation Loc,
mlir::Value V, clang::QualType Type,
clang::CharUnits Alignment,
clang::SanitizerSet SkippedChecks,
std::optional<mlir::Value> ArraySize) {
if (!sanitizePerformTypeCheck())
return;
assert(false && "type check NYI");
}
/// If the specified expression does not fold
/// to a constant, or if it does but contains a label, return false. If it
/// constant folds return true and set the folded value.
bool CIRGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond,
llvm::APSInt &ResultInt,
bool AllowLabels) {
// FIXME: Rename and handle conversion of other evaluatable things
// to bool.
Expr::EvalResult Result;
if (!Cond->EvaluateAsInt(Result, getContext()))
return false; // Not foldable, not integer or not fully evaluatable.
llvm::APSInt Int = Result.Val.getInt();
if (!AllowLabels && ContainsLabel(Cond))
return false; // Contains a label.
ResultInt = Int;
return true;
}
mlir::Type CIRGenFunction::getCIRType(const QualType &type) {
return CGM.getCIRType(type);
}
/// Determine whether the function F ends with a return stmt.
static bool endsWithReturn(const Decl *F) {
const Stmt *Body = nullptr;
if (auto *FD = dyn_cast_or_null<FunctionDecl>(F))
Body = FD->getBody();
else if (auto *OMD = dyn_cast_or_null<ObjCMethodDecl>(F))
llvm_unreachable("NYI");
if (auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) {
auto LastStmt = CS->body_rbegin();
if (LastStmt != CS->body_rend())
return isa<ReturnStmt>(*LastStmt);
}
return false;
}
void CIRGenFunction::emitAndUpdateRetAlloca(QualType ty, mlir::Location loc,
CharUnits alignment) {
if (ty->isVoidType()) {
// Void type; nothing to return.
ReturnValue = Address::invalid();
// Count the implicit return.
if (!endsWithReturn(CurFuncDecl))
++NumReturnExprs;
} else if (CurFnInfo->getReturnInfo().getKind() ==
cir::ABIArgInfo::Indirect) {
// TODO(CIR): Consider this implementation in CIRtoLLVM
llvm_unreachable("NYI");
// TODO(CIR): Consider this implementation in CIRtoLLVM
} else if (CurFnInfo->getReturnInfo().getKind() ==
cir::ABIArgInfo::InAlloca) {
llvm_unreachable("NYI");
} else {
auto addr = emitAlloca("__retval", ty, loc, alignment);
FnRetAlloca = addr;
ReturnValue = Address(addr, alignment);
// Tell the epilog emitter to autorelease the result. We do this now so
// that various specialized functions can suppress it during their IR -
// generation
if (getLangOpts().ObjCAutoRefCount)
llvm_unreachable("NYI");
}
}
mlir::LogicalResult CIRGenFunction::declare(const Decl *var, QualType ty,
mlir::Location loc,
CharUnits alignment,
mlir::Value &addr, bool isParam) {
const auto *namedVar = dyn_cast_or_null<NamedDecl>(var);
assert(namedVar && "Needs a named decl");
assert(!symbolTable.count(var) && "not supposed to be available just yet");
addr = emitAlloca(namedVar->getName(), ty, loc, alignment);
auto allocaOp = cast<cir::AllocaOp>(addr.getDefiningOp());
if (isParam)
allocaOp.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
if (ty->isReferenceType() || ty.isConstQualified())
allocaOp.setConstantAttr(mlir::UnitAttr::get(&getMLIRContext()));
symbolTable.insert(var, addr);
return mlir::success();
}
mlir::LogicalResult CIRGenFunction::declare(Address addr, const Decl *var,
QualType ty, mlir::Location loc,
CharUnits alignment,
mlir::Value &addrVal,
bool isParam) {
const auto *namedVar = dyn_cast_or_null<NamedDecl>(var);
assert(namedVar && "Needs a named decl");
assert(!symbolTable.count(var) && "not supposed to be available just yet");
addrVal = addr.getPointer();
auto allocaOp = cast<cir::AllocaOp>(addrVal.getDefiningOp());
if (isParam)
allocaOp.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
if (ty->isReferenceType() || ty.isConstQualified())
allocaOp.setConstantAttr(mlir::UnitAttr::get(&getMLIRContext()));
symbolTable.insert(var, addrVal);
return mlir::success();
}
/// All scope related cleanup needed:
/// - Patching up unsolved goto's.
/// - Build all cleanup code and insert yield/returns.
void CIRGenFunction::LexicalScope::cleanup() {
auto &builder = CGF.builder;
auto *localScope = CGF.currLexScope;
auto applyCleanup = [&]() {
if (PerformCleanup) {
// ApplyDebugLocation
assert(!cir::MissingFeatures::generateDebugInfo());
ForceCleanup();
}
};
// Cleanup are done right before codegen resume a scope. This is where
// objects are destroyed.
unsigned curLoc = 0;
for (auto *retBlock : localScope->getRetBlocks()) {
mlir::OpBuilder::InsertionGuard guard(builder);
builder.setInsertionPointToEnd(retBlock);
mlir::Location retLoc = *localScope->getRetLocs()[curLoc];
curLoc++;
(void)emitReturn(retLoc);
}
auto insertCleanupAndLeave = [&](mlir::Block *InsPt) {
mlir::OpBuilder::InsertionGuard guard(builder);
builder.setInsertionPointToEnd(InsPt);
// If we still don't have a cleanup block, it means that `applyCleanup`
// below might be able to get us one.
mlir::Block *cleanupBlock = localScope->getCleanupBlock(builder);
// Leverage and defers to RunCleanupsScope's dtor and scope handling.
applyCleanup();
// If we now have one after `applyCleanup`, hook it up properly.
if (!cleanupBlock && localScope->getCleanupBlock(builder)) {
cleanupBlock = localScope->getCleanupBlock(builder);
builder.create<BrOp>(InsPt->back().getLoc(), cleanupBlock);
}
if (localScope->Depth == 0) {
emitImplicitReturn();
return;
}
// End of any local scope != function
// Ternary ops have to deal with matching arms for yielding types
// and do return a value, it must do its own cir.yield insertion.
if (!localScope->isTernary() && !InsPt->mightHaveTerminator()) {
!retVal ? builder.create<YieldOp>(localScope->EndLoc)
: builder.create<YieldOp>(localScope->EndLoc, retVal);
}
};
// If a cleanup block has been created at some point, branch to it
// and set the insertion point to continue at the cleanup block.
// Terminators are then inserted either in the cleanup block or
// inline in this current block.
auto *cleanupBlock = localScope->getCleanupBlock(builder);
if (cleanupBlock)
insertCleanupAndLeave(cleanupBlock);
// Now deal with any pending block wrap up like implicit end of
// scope.
// If a terminator is already present in the current block, nothing
// else to do here.
auto *currBlock = builder.getBlock();
if (isGlobalInit() && !currBlock)
return;
if (currBlock->mightHaveTerminator() && currBlock->getTerminator())
return;
// An empty non-entry block has nothing to offer, and since this is
// synthetic, losing information does not affect anything.
bool entryBlock = builder.getInsertionBlock()->isEntryBlock();
if (!entryBlock && currBlock->empty() && currBlock->hasNoPredecessors()) {
currBlock->erase();
// Remove unused cleanup blocks.
if (cleanupBlock && cleanupBlock->hasNoPredecessors())
cleanupBlock->erase();
// FIXME(cir): ideally we should call applyCleanup() before we
// get into this condition and emit the proper cleanup. This is
// needed to get nrvo to interop with dtor logic.
PerformCleanup = false;
return;
}
// If there's a cleanup block, branch to it, nothing else to do.
if (cleanupBlock) {
builder.create<BrOp>(currBlock->back().getLoc(), cleanupBlock);
return;
}
// No pre-existent cleanup block, emit cleanup code and yield/return.
insertCleanupAndLeave(currBlock);
}
cir::ReturnOp CIRGenFunction::LexicalScope::emitReturn(mlir::Location loc) {
auto &builder = CGF.getBuilder();
// If we are on a coroutine, add the coro_end builtin call.
auto Fn = dyn_cast<cir::FuncOp>(CGF.CurFn);
assert(Fn && "other callables NYI");
if (Fn.getCoroutine())
CGF.emitCoroEndBuiltinCall(loc,
builder.getNullPtr(builder.getVoidPtrTy(), loc));
if (CGF.FnRetCIRTy.has_value()) {
// If there's anything to return, load it first.
auto val = builder.create<LoadOp>(loc, *CGF.FnRetCIRTy, *CGF.FnRetAlloca);
return builder.create<ReturnOp>(loc, llvm::ArrayRef(val.getResult()));
}
return builder.create<ReturnOp>(loc);
}
void CIRGenFunction::LexicalScope::emitImplicitReturn() {
auto &builder = CGF.getBuilder();
auto *localScope = CGF.currLexScope;
const auto *FD = cast<clang::FunctionDecl>(CGF.CurGD.getDecl());
// C++11 [stmt.return]p2:
// Flowing off the end of a function [...] results in undefined behavior
// in a value-returning function.
// C11 6.9.1p12:
// If the '}' that terminates a function is reached, and the value of the
// function call is used by the caller, the behavior is undefined.
if (CGF.getLangOpts().CPlusPlus && !FD->hasImplicitReturnZero() &&
!CGF.SawAsmBlock && !FD->getReturnType()->isVoidType() &&
builder.getInsertionBlock()) {
bool shouldEmitUnreachable = CGF.CGM.getCodeGenOpts().StrictReturn ||
!CGF.CGM.MayDropFunctionReturn(
FD->getASTContext(), FD->getReturnType());
if (CGF.SanOpts.has(SanitizerKind::Return)) {
assert(!cir::MissingFeatures::sanitizerReturn());
llvm_unreachable("NYI");
} else if (shouldEmitUnreachable) {
if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) {
builder.create<cir::TrapOp>(localScope->EndLoc);
builder.clearInsertionPoint();
return;
}
}
if (CGF.SanOpts.has(SanitizerKind::Return) || shouldEmitUnreachable) {
builder.create<cir::UnreachableOp>(localScope->EndLoc);
builder.clearInsertionPoint();
return;
}
}
(void)emitReturn(localScope->EndLoc);
}
cir::TryOp CIRGenFunction::LexicalScope::getClosestTryParent() {
auto *scope = this;
while (scope) {
if (scope->isTry())
return scope->getTry();
scope = scope->ParentScope;
}
return nullptr;
}
void CIRGenFunction::finishFunction(SourceLocation EndLoc) {
// CIRGen doesn't use a BreakContinueStack or evaluates OnlySimpleReturnStmts.
// Usually the return expression is evaluated before the cleanup
// code. If the function contains only a simple return statement,
// such as a constant, the location before the cleanup code becomes
// the last useful breakpoint in the function, because the simple
// return expression will be evaluated after the cleanup code. To be
// safe, set the debug location for cleanup code to the location of
// the return statement. Otherwise the cleanup code should be at the
// end of the function's lexical scope.
//
// If there are multiple branches to the return block, the branch
// instructions will get the location of the return statements and
// all will be fine.
if (auto *DI = getDebugInfo())
assert(!cir::MissingFeatures::generateDebugInfo() && "NYI");
// Pop any cleanups that might have been associated with the
// parameters. Do this in whatever block we're currently in; it's
// important to do this before we enter the return block or return
// edges will be *really* confused.
bool HasCleanups = EHStack.stable_begin() != PrologueCleanupDepth;
if (HasCleanups) {
// Make sure the line table doesn't jump back into the body for
// the ret after it's been at EndLoc.
if (auto *DI = getDebugInfo())
assert(!cir::MissingFeatures::generateDebugInfo() && "NYI");
builder.clearInsertionPoint();
PopCleanupBlocks(PrologueCleanupDepth);
}
// Emit function epilog (to return).
// Original LLVM codegen does EmitReturnBlock() here, CIRGen handles
// this as part of LexicalScope instead, given CIR might have multiple
// blocks with `cir.return`.
if (ShouldInstrumentFunction()) {
assert(!cir::MissingFeatures::shouldInstrumentFunction() && "NYI");
}
// Emit debug descriptor for function end.
if (auto *DI = getDebugInfo())
assert(!cir::MissingFeatures::generateDebugInfo() && "NYI");
// Reset the debug location to that of the simple 'return' expression, if any
// rather than that of the end of the function's scope '}'.
assert(!cir::MissingFeatures::generateDebugInfo() && "NYI");
assert(!cir::MissingFeatures::emitFunctionEpilog() && "NYI");
assert(!cir::MissingFeatures::emitEndEHSpec() && "NYI");
assert(EHStack.empty() && "did not remove all scopes from cleanup stack!");
// If someone did an indirect goto, emit the indirect goto block at the end of
// the function.
assert(!cir::MissingFeatures::indirectBranch() && "NYI");
// If some of our locals escaped, insert a call to llvm.localescape in the
// entry block.
assert(!cir::MissingFeatures::escapedLocals() && "NYI");
// If someone took the address of a label but never did an indirect goto, we
// made a zero entry PHI node, which is illegal, zap it now.
assert(!cir::MissingFeatures::indirectBranch() && "NYI");
// CIRGen doesn't need to emit EHResumeBlock, TerminateLandingPad,
// TerminateHandler, UnreachableBlock, TerminateFunclets, NormalCleanupDest
// here because the basic blocks aren't shared.
assert(!cir::MissingFeatures::emitDeclMetadata() && "NYI");
assert(!cir::MissingFeatures::deferredReplacements() && "NYI");
// Add the min-legal-vector-width attribute. This contains the max width from:
// 1. min-vector-width attribute used in the source program.
// 2. Any builtins used that have a vector width specified.
// 3. Values passed in and out of inline assembly.
// 4. Width of vector arguments and return types for this function.
// 5. Width of vector arguments and return types for functions called by
// this function.
assert(!cir::MissingFeatures::minLegalVectorWidthAttr() && "NYI");
// Add vscale_range attribute if appropriate.
assert(!cir::MissingFeatures::vscaleRangeAttr() && "NYI");
// In traditional LLVM codegen, if clang generated an unreachable return
// block, it'd be deleted now. Same for unused ret allocas from ReturnValue
}
cir::FuncOp CIRGenFunction::generateCode(clang::GlobalDecl GD, cir::FuncOp Fn,
const CIRGenFunctionInfo &FnInfo) {
assert(Fn && "generating code for a null function");
const auto FD = cast<FunctionDecl>(GD.getDecl());
CurGD = GD;
FnRetQualTy = FD->getReturnType();
if (!FnRetQualTy->isVoidType())
FnRetCIRTy = getCIRType(FnRetQualTy);
FunctionArgList Args;
QualType ResTy = buildFunctionArgList(GD, Args);
if (FD->isInlineBuiltinDeclaration()) {
llvm_unreachable("NYI");
} else {
// Detect the unusual situation where an inline version is shadowed by a
// non-inline version. In that case we should pick the external one
// everywhere. That's GCC behavior too. Unfortunately, I cannot find a way
// to detect that situation before we reach codegen, so do some late
// replacement.
for (const auto *PD = FD->getPreviousDecl(); PD;
PD = PD->getPreviousDecl()) {
if (LLVM_UNLIKELY(PD->isInlineBuiltinDeclaration())) {
llvm_unreachable("NYI");
}
}
}
// Check if we should generate debug info for this function.
if (FD->hasAttr<NoDebugAttr>()) {
assert(!cir::MissingFeatures::noDebugInfo());
}
// The function might not have a body if we're generating thunks for a
// function declaration.
SourceRange BodyRange;
if (Stmt *Body = FD->getBody())
BodyRange = Body->getSourceRange();
else
BodyRange = FD->getLocation();
// TODO: CurEHLocation
// Use the location of the start of the function to determine where the
// function definition is located. By default we use the location of the
// declaration as the location for the subprogram. A function may lack a
// declaration in the source code if it is created by code gen. (examples:
// _GLOBAL__I_a, __cxx_global_array_dtor, thunk).
SourceLocation Loc = FD->getLocation();
// If this is a function specialization then use the pattern body as the
// location for the function.
if (const auto *SpecDecl = FD->getTemplateInstantiationPattern())
if (SpecDecl->hasBody(SpecDecl))
Loc = SpecDecl->getLocation();
Stmt *Body = FD->getBody();
if (Body) {
// LLVM codegen: Coroutines always emit lifetime markers
// Hide this under request for lifetime emission so that we can write
// tests when the time comes, but CIR should be intrinsically scope
// accurate, so no need to tie coroutines to such markers.
if (isa<CoroutineBodyStmt>(Body))
assert(!cir::MissingFeatures::shouldEmitLifetimeMarkers() && "NYI");
// Initialize helper which will detect jumps which can cause invalid
// lifetime markers.
if (ShouldEmitLifetimeMarkers)
assert(!cir::MissingFeatures::shouldEmitLifetimeMarkers() && "NYI");
}
// Create a scope in the symbol table to hold variable declarations.
SymTableScopeTy varScope(symbolTable);
// Compiler synthetized functions might have invalid slocs...
auto bSrcLoc = FD->getBody()->getBeginLoc();
auto eSrcLoc = FD->getBody()->getEndLoc();
auto unknownLoc = builder.getUnknownLoc();
auto FnBeginLoc = bSrcLoc.isValid() ? getLoc(bSrcLoc) : unknownLoc;
auto FnEndLoc = eSrcLoc.isValid() ? getLoc(eSrcLoc) : unknownLoc;
const auto fusedLoc =
mlir::FusedLoc::get(&getMLIRContext(), {FnBeginLoc, FnEndLoc});
SourceLocRAIIObject fnLoc{*this, Loc.isValid() ? getLoc(Loc) : unknownLoc};
assert(Fn.isDeclaration() && "Function already has body?");
mlir::Block *EntryBB = Fn.addEntryBlock();
builder.setInsertionPointToStart(EntryBB);
{
// Initialize lexical scope information.
LexicalScope lexScope{*this, fusedLoc, EntryBB};
// Emit the standard function prologue.
StartFunction(GD, ResTy, Fn, FnInfo, Args, Loc, BodyRange.getBegin());
// Save parameters for coroutine function.
if (Body && isa_and_nonnull<CoroutineBodyStmt>(Body))
llvm::append_range(FnArgs, FD->parameters());
// Ensure that the function adheres to the forward progress guarantee, which
// is required by certain optimizations.
// In C++11 and up, the attribute will be removed if the body contains a
// trivial empty loop.
if (cir::MissingFeatures::mustProgress())
llvm_unreachable("NYI");
// Generate the body of the function.
// TODO: PGO.assignRegionCounters
assert(!cir::MissingFeatures::shouldInstrumentFunction());
if (isa<CXXDestructorDecl>(FD))
emitDestructorBody(Args);
else if (isa<CXXConstructorDecl>(FD))
emitConstructorBody(Args);
else if (getLangOpts().CUDA && !getLangOpts().CUDAIsDevice &&
FD->hasAttr<CUDAGlobalAttr>())
llvm_unreachable("NYI");
else if (isa<CXXMethodDecl>(FD) &&
cast<CXXMethodDecl>(FD)->isLambdaStaticInvoker()) {
// The lambda static invoker function is special, because it forwards or
// clones the body of the function call operator (but is actually
// static).
emitLambdaStaticInvokeBody(cast<CXXMethodDecl>(FD));
} else if (FD->isDefaulted() && isa<CXXMethodDecl>(FD) &&
(cast<CXXMethodDecl>(FD)->isCopyAssignmentOperator() ||
cast<CXXMethodDecl>(FD)->isMoveAssignmentOperator())) {
// Implicit copy-assignment gets the same special treatment as implicit
// copy-constructors.
emitImplicitAssignmentOperatorBody(Args);
} else if (Body) {
if (mlir::failed(emitFunctionBody(Body))) {
Fn.erase();
return nullptr;
}
} else
llvm_unreachable("no definition for emitted function");
assert(builder.getInsertionBlock() && "Should be valid");
}
if (mlir::failed(Fn.verifyBody()))
return nullptr;
// Emit the standard function epilogue.
finishFunction(BodyRange.getEnd());
// If we haven't marked the function nothrow through other means, do a quick
// pass now to see if we can.
assert(!cir::MissingFeatures::tryMarkNoThrow());
return Fn;
}
mlir::Value CIRGenFunction::createLoad(const VarDecl *VD, const char *Name) {
auto addr = GetAddrOfLocalVar(VD);
return builder.create<LoadOp>(getLoc(VD->getLocation()),
addr.getElementType(), addr.getPointer());
}
void CIRGenFunction::emitConstructorBody(FunctionArgList &Args) {
assert(!cir::MissingFeatures::emitAsanPrologueOrEpilogue());
const auto *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl());
auto CtorType = CurGD.getCtorType();
assert((CGM.getTarget().getCXXABI().hasConstructorVariants() ||
CtorType == Ctor_Complete) &&
"can only generate complete ctor for this ABI");
// Before we go any further, try the complete->base constructor delegation
// optimization.
if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor) &&
CGM.getTarget().getCXXABI().hasConstructorVariants()) {
emitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args, Ctor->getEndLoc());
return;
}
const FunctionDecl *Definition = nullptr;
Stmt *Body = Ctor->getBody(Definition);
assert(Definition == Ctor && "emitting wrong constructor body");
// Enter the function-try-block before the constructor prologue if
// applicable.
bool IsTryBody = (Body && isa<CXXTryStmt>(Body));
if (IsTryBody)
llvm_unreachable("NYI");
// TODO: incrementProfileCounter
// TODO: RunClenaupCcope RunCleanups(*this);
// TODO: in restricted cases, we can emit the vbase initializers of a
// complete ctor and then delegate to the base ctor.
// Emit the constructor prologue, i.e. the base and member initializers.
emitCtorPrologue(Ctor, CtorType, Args);
// Emit the body of the statement.
if (IsTryBody)
llvm_unreachable("NYI");
else {
// TODO: propagate this result via mlir::logical result. Just unreachable
// now just to have it handled.
if (mlir::failed(emitStmt(Body, true)))
llvm_unreachable("NYI");
}
// Emit any cleanup blocks associated with the member or base initializers,
// which inlcudes (along the exceptional path) the destructors for those
// members and bases that were fully constructed.
/// TODO: RunCleanups.ForceCleanup();
if (IsTryBody)
llvm_unreachable("NYI");
}
/// Given a value of type T* that may not be to a complete object, construct
/// an l-vlaue withi the natural pointee alignment of T.
LValue CIRGenFunction::MakeNaturalAlignPointeeAddrLValue(mlir::Value val,
QualType ty) {
// FIXME(cir): is it safe to assume Op->getResult(0) is valid? Perhaps
// assert on the result type first.
LValueBaseInfo baseInfo;
TBAAAccessInfo tbaaInfo;
CharUnits align = CGM.getNaturalTypeAlignment(ty, &baseInfo, &tbaaInfo,
/* for PointeeType= */ true);
return makeAddrLValue(Address(val, align), ty, baseInfo, tbaaInfo);
}
LValue CIRGenFunction::MakeNaturalAlignAddrLValue(mlir::Value val,
QualType ty) {
LValueBaseInfo baseInfo;
TBAAAccessInfo tbaaInfo;
CharUnits alignment = CGM.getNaturalTypeAlignment(ty, &baseInfo, &tbaaInfo);
Address addr(val, getTypes().convertTypeForMem(ty), alignment);
return LValue::makeAddr(addr, ty, getContext(), baseInfo, tbaaInfo);
}
// Map the LangOption for exception behavior into the corresponding enum in
// the IR.
cir::fp::ExceptionBehavior
ToConstrainedExceptMD(LangOptions::FPExceptionModeKind Kind) {
switch (Kind) {
case LangOptions::FPE_Ignore:
return cir::fp::ebIgnore;
case LangOptions::FPE_MayTrap:
return cir::fp::ebMayTrap;
case LangOptions::FPE_Strict:
return cir::fp::ebStrict;
default:
llvm_unreachable("Unsupported FP Exception Behavior");
}
}
bool CIRGenFunction::ShouldSkipSanitizerInstrumentation() {
if (!CurFuncDecl)
return false;
return CurFuncDecl->hasAttr<DisableSanitizerInstrumentationAttr>();
}
/// Return true if the current function should be instrumented with XRay nop
/// sleds.
bool CIRGenFunction::ShouldXRayInstrumentFunction() const {
return CGM.getCodeGenOpts().XRayInstrumentFunctions;
}
static bool matchesStlAllocatorFn(const Decl *D, const ASTContext &astContext) {
auto *MD = dyn_cast_or_null<CXXMethodDecl>(D);
if (!MD || !MD->getDeclName().getAsIdentifierInfo() ||
!MD->getDeclName().getAsIdentifierInfo()->isStr("allocate") ||
(MD->getNumParams() != 1 && MD->getNumParams() != 2))
return false;
if (MD->parameters()[0]->getType().getCanonicalType() !=
astContext.getSizeType())
return false;
if (MD->getNumParams() == 2) {
auto *PT = MD->parameters()[1]->getType()->getAs<clang::PointerType>();
if (!PT || !PT->isVoidPointerType() ||
!PT->getPointeeType().isConstQualified())
return false;
}
return true;
}
/// TODO: this should live in `emitFunctionProlog`
/// An argument came in as a promoted argument; demote it back to its
/// declared type.
static mlir::Value emitArgumentDemotion(CIRGenFunction &CGF, const VarDecl *var,
mlir::Value value) {
mlir::Type ty = CGF.ConvertType(var->getType());
// This can happen with promotions that actually don't change the
// underlying type, like the enum promotions.
if (value.getType() == ty)
return value;
assert((isa<cir::IntType>(ty) || cir::isAnyFloatingPointType(ty)) &&
"unexpected promotion type");
if (isa<cir::IntType>(ty))
return CGF.getBuilder().CIRBaseBuilderTy::createIntCast(value, ty);
return CGF.getBuilder().CIRBaseBuilderTy::createCast(cir::CastKind::floating,
value, ty);
}
void CIRGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
cir::FuncOp Fn,
const CIRGenFunctionInfo &FnInfo,
const FunctionArgList &Args,
SourceLocation Loc,
SourceLocation StartLoc) {
assert(!CurFn &&
"Do not use a CIRGenFunction object for more than one function");
const auto *D = GD.getDecl();
DidCallStackSave = false;
CurCodeDecl = D;
const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
if (FD && FD->usesSEHTry())
CurSEHParent = GD;
CurFuncDecl = (D ? D->getNonClosureContext() : nullptr);
FnRetTy = RetTy;
CurFn = Fn;
CurFnInfo = &FnInfo;
// If this function is ignored for any of the enabled sanitizers, disable
// the sanitizer for the function.
do {
#define SANITIZER(NAME, ID) \
if (SanOpts.empty()) \
break; \
if (SanOpts.has(SanitizerKind::ID)) \
if (CGM.isInNoSanitizeList(SanitizerKind::ID, Fn, Loc)) \
SanOpts.set(SanitizerKind::ID, false);
#include "clang/Basic/Sanitizers.def"
#undef SANITIZER
} while (false);
if (D) {
const bool SanitizeBounds = SanOpts.hasOneOf(SanitizerKind::Bounds);
SanitizerMask no_sanitize_mask;
bool NoSanitizeCoverage = false;
for (auto *Attr : D->specific_attrs<NoSanitizeAttr>()) {
no_sanitize_mask |= Attr->getMask();
// SanitizeCoverage is not handled by SanOpts.
if (Attr->hasCoverage())
NoSanitizeCoverage = true;
}
// Apply the no_sanitize* attributes to SanOpts.
SanOpts.Mask &= ~no_sanitize_mask;
if (no_sanitize_mask & SanitizerKind::Address)
SanOpts.set(SanitizerKind::KernelAddress, false);
if (no_sanitize_mask & SanitizerKind::KernelAddress)
SanOpts.set(SanitizerKind::Address, false);
if (no_sanitize_mask & SanitizerKind::HWAddress)
SanOpts.set(SanitizerKind::KernelHWAddress, false);
if (no_sanitize_mask & SanitizerKind::KernelHWAddress)
SanOpts.set(SanitizerKind::HWAddress, false);
// TODO(cir): set llvm::Attribute::NoSanitizeBounds
if (SanitizeBounds && !SanOpts.hasOneOf(SanitizerKind::Bounds))
assert(!cir::MissingFeatures::sanitizeOther());
// TODO(cir): set llvm::Attribute::NoSanitizeCoverage
if (NoSanitizeCoverage && CGM.getCodeGenOpts().hasSanitizeCoverage())
assert(!cir::MissingFeatures::sanitizeOther());
// Some passes need the non-negated no_sanitize attribute. Pass them on.
if (CGM.getCodeGenOpts().hasSanitizeBinaryMetadata()) {
// TODO(cir): set no_sanitize_thread
if (no_sanitize_mask & SanitizerKind::Thread)
assert(!cir::MissingFeatures::sanitizeOther());
}
}
if (ShouldSkipSanitizerInstrumentation()) {
assert(!cir::MissingFeatures::sanitizeOther());
} else {
// Apply sanitizer attributes to the function.
if (SanOpts.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress))
assert(!cir::MissingFeatures::sanitizeOther());
if (SanOpts.hasOneOf(SanitizerKind::HWAddress |
SanitizerKind::KernelHWAddress))
assert(!cir::MissingFeatures::sanitizeOther());
if (SanOpts.has(SanitizerKind::MemtagStack))
assert(!cir::MissingFeatures::sanitizeOther());
if (SanOpts.has(SanitizerKind::Thread))
assert(!cir::MissingFeatures::sanitizeOther());
if (SanOpts.has(SanitizerKind::NumericalStability))
assert(!cir::MissingFeatures::sanitizeOther());
if (SanOpts.hasOneOf(SanitizerKind::Memory | SanitizerKind::KernelMemory))
assert(!cir::MissingFeatures::sanitizeOther());
}