-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathLoweringPrepare.cpp
1219 lines (1047 loc) · 44.5 KB
/
LoweringPrepare.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
//===- LoweringPrepare.cpp - pareparation work for LLVM lowering ----------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "LoweringPrepareCXXABI.h"
#include "PassDetail.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/Region.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/CharUnits.h"
#include "clang/AST/Mangle.h"
#include "clang/Basic/Module.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h"
#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Path.h"
#include <memory>
using cir::CIRBaseBuilderTy;
using namespace mlir;
using namespace cir;
static SmallString<128> getTransformedFileName(ModuleOp theModule) {
SmallString<128> FileName;
if (theModule.getSymName()) {
FileName = llvm::sys::path::filename(theModule.getSymName()->str());
}
if (FileName.empty())
FileName = "<null>";
for (size_t i = 0; i < FileName.size(); ++i) {
// Replace everything that's not [a-zA-Z0-9._] with a _. This set happens
// to be the set of C preprocessing numbers.
if (!clang::isPreprocessingNumberBody(FileName[i]))
FileName[i] = '_';
}
return FileName;
}
/// Return the FuncOp called by `callOp`.
static FuncOp getCalledFunction(CallOp callOp) {
SymbolRefAttr sym =
llvm::dyn_cast_if_present<SymbolRefAttr>(callOp.getCallableForCallee());
if (!sym)
return nullptr;
return dyn_cast_or_null<FuncOp>(
SymbolTable::lookupNearestSymbolFrom(callOp, sym));
}
namespace {
struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
LoweringPreparePass() = default;
void runOnOperation() override;
void runOnOp(Operation *op);
void lowerUnaryOp(UnaryOp op);
void lowerBinOp(BinOp op);
void lowerCastOp(CastOp op);
void lowerComplexBinOp(ComplexBinOp op);
void lowerThreeWayCmpOp(CmpThreeWayOp op);
void lowerVAArgOp(VAArgOp op);
void lowerGlobalOp(GlobalOp op);
void lowerDynamicCastOp(DynamicCastOp op);
void lowerStdFindOp(StdFindOp op);
void lowerIterBeginOp(IterBeginOp op);
void lowerIterEndOp(IterEndOp op);
void lowerToMemCpy(StoreOp op);
void lowerArrayDtor(ArrayDtor op);
void lowerArrayCtor(ArrayCtor op);
/// Collect annotations of global values in the module
void addGlobalAnnotations(mlir::Operation *op, mlir::ArrayAttr annotations);
/// Build the function that initializes the specified global
FuncOp buildCXXGlobalVarDeclInitFunc(GlobalOp op);
/// Build a module init function that calls all the dynamic initializers.
void buildCXXGlobalInitFunc();
/// Materialize global ctor/dtor list
void buildGlobalCtorDtorList();
/// Build attribute of global annotation values
void buildGlobalAnnotationValues();
FuncOp buildRuntimeFunction(
mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
cir::FuncType type,
cir::GlobalLinkageKind linkage = cir::GlobalLinkageKind::ExternalLinkage);
GlobalOp buildRuntimeVariable(
mlir::OpBuilder &Builder, llvm::StringRef Name, mlir::Location Loc,
mlir::Type type,
cir::GlobalLinkageKind Linkage = cir::GlobalLinkageKind::ExternalLinkage);
/// Track the current number of global array string count for when the symbol
/// has an empty name, and prevent collisions.
uint64_t annonGlobalConstArrayCount = 0;
///
/// AST related
/// -----------
clang::ASTContext *astCtx;
std::shared_ptr<cir::LoweringPrepareCXXABI> cxxABI;
void setASTContext(clang::ASTContext *c) {
astCtx = c;
const clang::TargetInfo &target = c->getTargetInfo();
auto abiStr = target.getABI();
switch (c->getCXXABIKind()) {
case clang::TargetCXXABI::GenericItanium:
if (target.getTriple().getArch() == llvm::Triple::x86_64) {
cxxABI.reset(
cir::LoweringPrepareCXXABI::createX86ABI(/*is64bit=*/true));
break;
}
cxxABI.reset(cir::LoweringPrepareCXXABI::createItaniumABI());
break;
case clang::TargetCXXABI::GenericAArch64:
case clang::TargetCXXABI::AppleARM64:
// TODO: This is temporary solution. ABIKind info should be
// propagated from the targetInfo managed by ABI lowering
// query system.
assert(abiStr == "aapcs" || abiStr == "darwinpcs" ||
abiStr == "aapcs-soft");
cxxABI.reset(cir::LoweringPrepareCXXABI::createAArch64ABI(
abiStr == "aapcs"
? cir::AArch64ABIKind::AAPCS
: (abiStr == "darwinpccs" ? cir::AArch64ABIKind::DarwinPCS
: cir::AArch64ABIKind::AAPCSSoft)));
break;
default:
llvm_unreachable("NYI");
}
}
/// Tracks current module.
ModuleOp theModule;
/// Tracks existing dynamic initializers.
llvm::StringMap<uint32_t> dynamicInitializerNames;
llvm::SmallVector<FuncOp, 4> dynamicInitializers;
/// List of ctors to be called before main()
llvm::SmallVector<mlir::Attribute, 4> globalCtorList;
/// List of dtors to be called when unloading module.
llvm::SmallVector<mlir::Attribute, 4> globalDtorList;
/// List of annotations in the module
llvm::SmallVector<mlir::Attribute, 4> globalAnnotations;
};
} // namespace
GlobalOp LoweringPreparePass::buildRuntimeVariable(
mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
mlir::Type type, cir::GlobalLinkageKind linkage) {
GlobalOp g = dyn_cast_or_null<GlobalOp>(SymbolTable::lookupNearestSymbolFrom(
theModule, StringAttr::get(theModule->getContext(), name)));
if (!g) {
g = builder.create<cir::GlobalOp>(loc, name, type);
g.setLinkageAttr(
cir::GlobalLinkageKindAttr::get(builder.getContext(), linkage));
mlir::SymbolTable::setSymbolVisibility(
g, mlir::SymbolTable::Visibility::Private);
}
return g;
}
FuncOp LoweringPreparePass::buildRuntimeFunction(
mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
cir::FuncType type, cir::GlobalLinkageKind linkage) {
FuncOp f = dyn_cast_or_null<FuncOp>(SymbolTable::lookupNearestSymbolFrom(
theModule, StringAttr::get(theModule->getContext(), name)));
if (!f) {
f = builder.create<cir::FuncOp>(loc, name, type);
f.setLinkageAttr(
cir::GlobalLinkageKindAttr::get(builder.getContext(), linkage));
mlir::SymbolTable::setSymbolVisibility(
f, mlir::SymbolTable::Visibility::Private);
mlir::NamedAttrList attrs;
f.setExtraAttrsAttr(cir::ExtraFuncAttributesAttr::get(
builder.getContext(), attrs.getDictionary(builder.getContext())));
}
return f;
}
FuncOp LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(GlobalOp op) {
SmallString<256> fnName;
{
llvm::raw_svector_ostream Out(fnName);
op.getAst()->mangleDynamicInitializer(Out);
// Name numbering
uint32_t cnt = dynamicInitializerNames[fnName]++;
if (cnt)
fnName += "." + llvm::Twine(cnt).str();
}
// Create a variable initialization function.
CIRBaseBuilderTy builder(getContext());
builder.setInsertionPointAfter(op);
auto voidTy = cir::VoidType::get(builder.getContext());
auto fnType = cir::FuncType::get({}, voidTy);
FuncOp f = buildRuntimeFunction(builder, fnName, op.getLoc(), fnType,
cir::GlobalLinkageKind::InternalLinkage);
// Move over the initialzation code of the ctor region.
auto &block = op.getCtorRegion().front();
mlir::Block *entryBB = f.addEntryBlock();
entryBB->getOperations().splice(entryBB->begin(), block.getOperations(),
block.begin(), std::prev(block.end()));
// Register the destructor call with __cxa_atexit
auto &dtorRegion = op.getDtorRegion();
if (!dtorRegion.empty()) {
assert(op.getAst() &&
op.getAst()->getTLSKind() == clang::VarDecl::TLS_None && " TLS NYI");
// Create a variable that binds the atexit to this shared object.
builder.setInsertionPointToStart(&theModule.getBodyRegion().front());
auto Handle = buildRuntimeVariable(builder, "__dso_handle", op.getLoc(),
builder.getI8Type());
// Look for the destructor call in dtorBlock
auto &dtorBlock = dtorRegion.front();
cir::CallOp dtorCall;
for (auto op : reverse(dtorBlock.getOps<cir::CallOp>())) {
dtorCall = op;
break;
}
assert(dtorCall && "Expected a dtor call");
FuncOp dtorFunc = getCalledFunction(dtorCall);
assert(dtorFunc &&
mlir::isa<ASTCXXDestructorDeclInterface>(*dtorFunc.getAst()) &&
"Expected a dtor call");
// Create a runtime helper function:
// extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
auto voidPtrTy = cir::PointerType::get(builder.getContext(), voidTy);
auto voidFnTy = cir::FuncType::get({voidPtrTy}, voidTy);
auto voidFnPtrTy = cir::PointerType::get(builder.getContext(), voidFnTy);
auto HandlePtrTy =
cir::PointerType::get(builder.getContext(), Handle.getSymType());
auto fnAtExitType =
cir::FuncType::get({voidFnPtrTy, voidPtrTy, HandlePtrTy},
cir::VoidType::get(builder.getContext()));
const char *nameAtExit = "__cxa_atexit";
FuncOp fnAtExit =
buildRuntimeFunction(builder, nameAtExit, op.getLoc(), fnAtExitType);
// Replace the dtor call with a call to __cxa_atexit(&dtor, &var,
// &__dso_handle)
builder.setInsertionPointAfter(dtorCall);
mlir::Value args[3];
auto dtorPtrTy =
cir::PointerType::get(builder.getContext(), dtorFunc.getFunctionType());
// dtorPtrTy
args[0] = builder.create<cir::GetGlobalOp>(dtorCall.getLoc(), dtorPtrTy,
dtorFunc.getSymName());
args[0] = builder.create<cir::CastOp>(dtorCall.getLoc(), voidFnPtrTy,
cir::CastKind::bitcast, args[0]);
args[1] = builder.create<cir::CastOp>(dtorCall.getLoc(), voidPtrTy,
cir::CastKind::bitcast,
dtorCall.getArgOperand(0));
args[2] = builder.create<cir::GetGlobalOp>(Handle.getLoc(), HandlePtrTy,
Handle.getSymName());
builder.createCallOp(dtorCall.getLoc(), fnAtExit, args);
dtorCall->erase();
entryBB->getOperations().splice(entryBB->end(), dtorBlock.getOperations(),
dtorBlock.begin(),
std::prev(dtorBlock.end()));
}
// Replace cir.yield with cir.return
builder.setInsertionPointToEnd(entryBB);
auto &yieldOp = block.getOperations().back();
assert(isa<YieldOp>(yieldOp));
builder.create<ReturnOp>(yieldOp.getLoc());
return f;
}
static void canonicalizeIntrinsicThreeWayCmp(CIRBaseBuilderTy &builder,
CmpThreeWayOp op) {
auto loc = op->getLoc();
auto cmpInfo = op.getInfo();
if (cmpInfo.getLt() == -1 && cmpInfo.getEq() == 0 && cmpInfo.getGt() == 1) {
// The comparison is already in canonicalized form.
return;
}
auto canonicalizedCmpInfo =
cir::CmpThreeWayInfoAttr::get(builder.getContext(), -1, 0, 1);
mlir::Value result =
builder
.create<cir::CmpThreeWayOp>(loc, op.getType(), op.getLhs(),
op.getRhs(), canonicalizedCmpInfo)
.getResult();
auto compareAndYield = [&](mlir::Value input, int64_t test,
int64_t yield) -> mlir::Value {
// Create a conditional branch that tests whether `input` is equal to
// `test`. If `input` is equal to `test`, yield `yield`. Otherwise, yield
// `input` as is.
auto testValue =
builder.getConstant(loc, cir::IntAttr::get(input.getType(), test));
auto yieldValue =
builder.getConstant(loc, cir::IntAttr::get(input.getType(), yield));
auto eqToTest =
builder.createCompare(loc, cir::CmpOpKind::eq, input, testValue);
return builder.createSelect(loc, eqToTest, yieldValue, input);
};
if (cmpInfo.getLt() != -1)
result = compareAndYield(result, -1, cmpInfo.getLt());
if (cmpInfo.getEq() != 0)
result = compareAndYield(result, 0, cmpInfo.getEq());
if (cmpInfo.getGt() != 1)
result = compareAndYield(result, 1, cmpInfo.getGt());
op.replaceAllUsesWith(result);
op.erase();
}
void LoweringPreparePass::lowerVAArgOp(VAArgOp op) {
CIRBaseBuilderTy builder(getContext());
builder.setInsertionPoint(op);
cir::CIRDataLayout datalayout(theModule);
auto res = cxxABI->lowerVAArg(builder, op, datalayout);
if (res) {
op.replaceAllUsesWith(res);
op.erase();
}
return;
}
void LoweringPreparePass::lowerUnaryOp(UnaryOp op) {
auto ty = op.getType();
if (!mlir::isa<cir::ComplexType>(ty))
return;
auto loc = op.getLoc();
auto opKind = op.getKind();
CIRBaseBuilderTy builder(getContext());
builder.setInsertionPointAfter(op);
auto operand = op.getInput();
auto operandReal = builder.createComplexReal(loc, operand);
auto operandImag = builder.createComplexImag(loc, operand);
mlir::Value resultReal;
mlir::Value resultImag;
switch (opKind) {
case cir::UnaryOpKind::Inc:
case cir::UnaryOpKind::Dec:
resultReal = builder.createUnaryOp(loc, opKind, operandReal);
resultImag = operandImag;
break;
case cir::UnaryOpKind::Plus:
case cir::UnaryOpKind::Minus:
resultReal = builder.createUnaryOp(loc, opKind, operandReal);
resultImag = builder.createUnaryOp(loc, opKind, operandImag);
break;
case cir::UnaryOpKind::Not:
resultReal = operandReal;
resultImag =
builder.createUnaryOp(loc, cir::UnaryOpKind::Minus, operandImag);
break;
}
auto result = builder.createComplexCreate(loc, resultReal, resultImag);
op.replaceAllUsesWith(result);
op.erase();
}
void LoweringPreparePass::lowerBinOp(BinOp op) {
auto ty = op.getType();
if (!mlir::isa<cir::ComplexType>(ty))
return;
auto loc = op.getLoc();
auto opKind = op.getKind();
assert((opKind == cir::BinOpKind::Add || opKind == cir::BinOpKind::Sub) &&
"invalid binary op kind on complex numbers");
CIRBaseBuilderTy builder(getContext());
builder.setInsertionPointAfter(op);
auto lhs = op.getLhs();
auto rhs = op.getRhs();
// (a+bi) + (c+di) = (a+c) + (b+d)i
// (a+bi) - (c+di) = (a-c) + (b-d)i
auto lhsReal = builder.createComplexReal(loc, lhs);
auto lhsImag = builder.createComplexImag(loc, lhs);
auto rhsReal = builder.createComplexReal(loc, rhs);
auto rhsImag = builder.createComplexImag(loc, rhs);
auto resultReal = builder.createBinop(lhsReal, opKind, rhsReal);
auto resultImag = builder.createBinop(lhsImag, opKind, rhsImag);
auto result = builder.createComplexCreate(loc, resultReal, resultImag);
op.replaceAllUsesWith(result);
op.erase();
}
static mlir::Value lowerScalarToComplexCast(MLIRContext &ctx, CastOp op) {
CIRBaseBuilderTy builder(ctx);
builder.setInsertionPoint(op);
auto src = op.getSrc();
auto imag = builder.getNullValue(src.getType(), op.getLoc());
return builder.createComplexCreate(op.getLoc(), src, imag);
}
static mlir::Value lowerComplexToScalarCast(MLIRContext &ctx, CastOp op) {
CIRBaseBuilderTy builder(ctx);
builder.setInsertionPoint(op);
auto src = op.getSrc();
if (!mlir::isa<cir::BoolType>(op.getType()))
return builder.createComplexReal(op.getLoc(), src);
// Complex cast to bool: (bool)(a+bi) => (bool)a || (bool)b
auto srcReal = builder.createComplexReal(op.getLoc(), src);
auto srcImag = builder.createComplexImag(op.getLoc(), src);
cir::CastKind elemToBoolKind;
if (op.getKind() == cir::CastKind::float_complex_to_bool)
elemToBoolKind = cir::CastKind::float_to_bool;
else if (op.getKind() == cir::CastKind::int_complex_to_bool)
elemToBoolKind = cir::CastKind::int_to_bool;
else
llvm_unreachable("invalid complex to bool cast kind");
auto boolTy = builder.getBoolTy();
auto srcRealToBool =
builder.createCast(op.getLoc(), elemToBoolKind, srcReal, boolTy);
auto srcImagToBool =
builder.createCast(op.getLoc(), elemToBoolKind, srcImag, boolTy);
// srcRealToBool || srcImagToBool
return builder.createLogicalOr(op.getLoc(), srcRealToBool, srcImagToBool);
}
static mlir::Value lowerComplexToComplexCast(MLIRContext &ctx, CastOp op) {
CIRBaseBuilderTy builder(ctx);
builder.setInsertionPoint(op);
auto src = op.getSrc();
auto dstComplexElemTy =
mlir::cast<cir::ComplexType>(op.getType()).getElementTy();
auto srcReal = builder.createComplexReal(op.getLoc(), src);
auto srcImag = builder.createComplexReal(op.getLoc(), src);
cir::CastKind scalarCastKind;
switch (op.getKind()) {
case cir::CastKind::float_complex:
scalarCastKind = cir::CastKind::floating;
break;
case cir::CastKind::float_complex_to_int_complex:
scalarCastKind = cir::CastKind::float_to_int;
break;
case cir::CastKind::int_complex:
scalarCastKind = cir::CastKind::integral;
break;
case cir::CastKind::int_complex_to_float_complex:
scalarCastKind = cir::CastKind::int_to_float;
break;
default:
llvm_unreachable("invalid complex to complex cast kind");
}
auto dstReal = builder.createCast(op.getLoc(), scalarCastKind, srcReal,
dstComplexElemTy);
auto dstImag = builder.createCast(op.getLoc(), scalarCastKind, srcImag,
dstComplexElemTy);
return builder.createComplexCreate(op.getLoc(), dstReal, dstImag);
}
void LoweringPreparePass::lowerCastOp(CastOp op) {
mlir::Value loweredValue;
switch (op.getKind()) {
case cir::CastKind::float_to_complex:
case cir::CastKind::int_to_complex:
loweredValue = lowerScalarToComplexCast(getContext(), op);
break;
case cir::CastKind::float_complex_to_real:
case cir::CastKind::int_complex_to_real:
case cir::CastKind::float_complex_to_bool:
case cir::CastKind::int_complex_to_bool:
loweredValue = lowerComplexToScalarCast(getContext(), op);
break;
case cir::CastKind::float_complex:
case cir::CastKind::float_complex_to_int_complex:
case cir::CastKind::int_complex:
case cir::CastKind::int_complex_to_float_complex:
loweredValue = lowerComplexToComplexCast(getContext(), op);
break;
default:
return;
}
op.replaceAllUsesWith(loweredValue);
op.erase();
}
static mlir::Value buildComplexBinOpLibCall(
LoweringPreparePass &pass, CIRBaseBuilderTy &builder,
llvm::StringRef (*libFuncNameGetter)(llvm::APFloat::Semantics),
mlir::Location loc, cir::ComplexType ty, mlir::Value lhsReal,
mlir::Value lhsImag, mlir::Value rhsReal, mlir::Value rhsImag) {
auto elementTy = mlir::cast<cir::CIRFPTypeInterface>(ty.getElementTy());
auto libFuncName = libFuncNameGetter(
llvm::APFloat::SemanticsToEnum(elementTy.getFloatSemantics()));
llvm::SmallVector<mlir::Type, 4> libFuncInputTypes(4, elementTy);
auto libFuncTy = cir::FuncType::get(libFuncInputTypes, ty);
cir::FuncOp libFunc;
{
mlir::OpBuilder::InsertionGuard ipGuard{builder};
builder.setInsertionPointToStart(pass.theModule.getBody());
libFunc = pass.buildRuntimeFunction(builder, libFuncName, loc, libFuncTy);
}
auto call =
builder.createCallOp(loc, libFunc, {lhsReal, lhsImag, rhsReal, rhsImag});
return call.getResult();
}
static llvm::StringRef
getComplexMulLibCallName(llvm::APFloat::Semantics semantics) {
switch (semantics) {
case llvm::APFloat::S_IEEEhalf:
return "__mulhc3";
case llvm::APFloat::S_IEEEsingle:
return "__mulsc3";
case llvm::APFloat::S_IEEEdouble:
return "__muldc3";
case llvm::APFloat::S_PPCDoubleDouble:
return "__multc3";
case llvm::APFloat::S_x87DoubleExtended:
return "__mulxc3";
case llvm::APFloat::S_IEEEquad:
return "__multc3";
default:
llvm_unreachable("unsupported floating point type");
}
}
static llvm::StringRef
getComplexDivLibCallName(llvm::APFloat::Semantics semantics) {
switch (semantics) {
case llvm::APFloat::S_IEEEhalf:
return "__divhc3";
case llvm::APFloat::S_IEEEsingle:
return "__divsc3";
case llvm::APFloat::S_IEEEdouble:
return "__divdc3";
case llvm::APFloat::S_PPCDoubleDouble:
return "__divtc3";
case llvm::APFloat::S_x87DoubleExtended:
return "__divxc3";
case llvm::APFloat::S_IEEEquad:
return "__divtc3";
default:
llvm_unreachable("unsupported floating point type");
}
}
static mlir::Value lowerComplexMul(LoweringPreparePass &pass,
CIRBaseBuilderTy &builder,
mlir::Location loc, cir::ComplexBinOp op,
mlir::Value lhsReal, mlir::Value lhsImag,
mlir::Value rhsReal, mlir::Value rhsImag) {
// (a+bi) * (c+di) = (ac-bd) + (ad+bc)i
auto resultRealLhs =
builder.createBinop(lhsReal, cir::BinOpKind::Mul, rhsReal);
auto resultRealRhs =
builder.createBinop(lhsImag, cir::BinOpKind::Mul, rhsImag);
auto resultImagLhs =
builder.createBinop(lhsReal, cir::BinOpKind::Mul, rhsImag);
auto resultImagRhs =
builder.createBinop(lhsImag, cir::BinOpKind::Mul, rhsReal);
auto resultReal =
builder.createBinop(resultRealLhs, cir::BinOpKind::Sub, resultRealRhs);
auto resultImag =
builder.createBinop(resultImagLhs, cir::BinOpKind::Add, resultImagRhs);
auto algebraicResult =
builder.createComplexCreate(loc, resultReal, resultImag);
auto ty = op.getType();
auto range = op.getRange();
if (mlir::isa<cir::IntType>(ty.getElementTy()) ||
range == cir::ComplexRangeKind::Basic ||
range == cir::ComplexRangeKind::Improved ||
range == cir::ComplexRangeKind::Promoted)
return algebraicResult;
// Check whether the real part and the imaginary part of the result are both
// NaN. If so, emit a library call to compute the multiplication instead.
// We check a value against NaN by comparing the value against itself.
auto resultRealIsNaN = builder.createIsNaN(loc, resultReal);
auto resultImagIsNaN = builder.createIsNaN(loc, resultImag);
auto resultRealAndImagAreNaN =
builder.createLogicalAnd(loc, resultRealIsNaN, resultImagIsNaN);
return builder
.create<cir::TernaryOp>(
loc, resultRealAndImagAreNaN,
[&](mlir::OpBuilder &, mlir::Location) {
auto libCallResult = buildComplexBinOpLibCall(
pass, builder, &getComplexMulLibCallName, loc, ty, lhsReal,
lhsImag, rhsReal, rhsImag);
builder.createYield(loc, libCallResult);
},
[&](mlir::OpBuilder &, mlir::Location) {
builder.createYield(loc, algebraicResult);
})
.getResult();
}
static mlir::Value
buildAlgebraicComplexDiv(CIRBaseBuilderTy &builder, mlir::Location loc,
mlir::Value lhsReal, mlir::Value lhsImag,
mlir::Value rhsReal, mlir::Value rhsImag) {
// (a+bi) / (c+di) = ((ac+bd)/(cc+dd)) + ((bc-ad)/(cc+dd))i
auto &a = lhsReal;
auto &b = lhsImag;
auto &c = rhsReal;
auto &d = rhsImag;
auto ac = builder.createBinop(loc, a, cir::BinOpKind::Mul, c); // a*c
auto bd = builder.createBinop(loc, b, cir::BinOpKind::Mul, d); // b*d
auto cc = builder.createBinop(loc, c, cir::BinOpKind::Mul, c); // c*c
auto dd = builder.createBinop(loc, d, cir::BinOpKind::Mul, d); // d*d
auto acbd = builder.createBinop(loc, ac, cir::BinOpKind::Add, bd); // ac+bd
auto ccdd = builder.createBinop(loc, cc, cir::BinOpKind::Add, dd); // cc+dd
auto resultReal = builder.createBinop(loc, acbd, cir::BinOpKind::Div, ccdd);
auto bc = builder.createBinop(loc, b, cir::BinOpKind::Mul, c); // b*c
auto ad = builder.createBinop(loc, a, cir::BinOpKind::Mul, d); // a*d
auto bcad = builder.createBinop(loc, bc, cir::BinOpKind::Sub, ad); // bc-ad
auto resultImag = builder.createBinop(loc, bcad, cir::BinOpKind::Div, ccdd);
return builder.createComplexCreate(loc, resultReal, resultImag);
}
static mlir::Value
buildRangeReductionComplexDiv(CIRBaseBuilderTy &builder, mlir::Location loc,
mlir::Value lhsReal, mlir::Value lhsImag,
mlir::Value rhsReal, mlir::Value rhsImag) {
// Implements Smith's algorithm for complex division.
// SMITH, R. L. Algorithm 116: Complex division. Commun. ACM 5, 8 (1962).
// Let:
// - lhs := a+bi
// - rhs := c+di
// - result := lhs / rhs = e+fi
//
// The algorithm psudocode looks like follows:
// if fabs(c) >= fabs(d):
// r := d / c
// tmp := c + r*d
// e = (a + b*r) / tmp
// f = (b - a*r) / tmp
// else:
// r := c / d
// tmp := d + r*c
// e = (a*r + b) / tmp
// f = (b*r - a) / tmp
auto &a = lhsReal;
auto &b = lhsImag;
auto &c = rhsReal;
auto &d = rhsImag;
auto trueBranchBuilder = [&](mlir::OpBuilder &, mlir::Location) {
auto r = builder.createBinop(loc, d, cir::BinOpKind::Div,
c); // r := d / c
auto rd = builder.createBinop(loc, r, cir::BinOpKind::Mul, d); // r*d
auto tmp = builder.createBinop(loc, c, cir::BinOpKind::Add,
rd); // tmp := c + r*d
auto br = builder.createBinop(loc, b, cir::BinOpKind::Mul, r); // b*r
auto abr = builder.createBinop(loc, a, cir::BinOpKind::Add, br); // a + b*r
auto e = builder.createBinop(loc, abr, cir::BinOpKind::Div, tmp);
auto ar = builder.createBinop(loc, a, cir::BinOpKind::Mul, r); // a*r
auto bar = builder.createBinop(loc, b, cir::BinOpKind::Sub, ar); // b - a*r
auto f = builder.createBinop(loc, bar, cir::BinOpKind::Div, tmp);
auto result = builder.createComplexCreate(loc, e, f);
builder.createYield(loc, result);
};
auto falseBranchBuilder = [&](mlir::OpBuilder &, mlir::Location) {
auto r = builder.createBinop(loc, c, cir::BinOpKind::Div,
d); // r := c / d
auto rc = builder.createBinop(loc, r, cir::BinOpKind::Mul, c); // r*c
auto tmp = builder.createBinop(loc, d, cir::BinOpKind::Add,
rc); // tmp := d + r*c
auto ar = builder.createBinop(loc, a, cir::BinOpKind::Mul, r); // a*r
auto arb = builder.createBinop(loc, ar, cir::BinOpKind::Add, b); // a*r + b
auto e = builder.createBinop(loc, arb, cir::BinOpKind::Div, tmp);
auto br = builder.createBinop(loc, b, cir::BinOpKind::Mul, r); // b*r
auto bra = builder.createBinop(loc, br, cir::BinOpKind::Sub, a); // b*r - a
auto f = builder.createBinop(loc, bra, cir::BinOpKind::Div, tmp);
auto result = builder.createComplexCreate(loc, e, f);
builder.createYield(loc, result);
};
auto cFabs = builder.create<cir::FAbsOp>(loc, c);
auto dFabs = builder.create<cir::FAbsOp>(loc, d);
auto cmpResult = builder.createCompare(loc, cir::CmpOpKind::ge, cFabs, dFabs);
auto ternary = builder.create<cir::TernaryOp>(
loc, cmpResult, trueBranchBuilder, falseBranchBuilder);
return ternary.getResult();
}
static mlir::Value lowerComplexDiv(LoweringPreparePass &pass,
CIRBaseBuilderTy &builder,
mlir::Location loc, cir::ComplexBinOp op,
mlir::Value lhsReal, mlir::Value lhsImag,
mlir::Value rhsReal, mlir::Value rhsImag) {
auto ty = op.getType();
if (mlir::isa<cir::CIRFPTypeInterface>(ty.getElementTy())) {
auto range = op.getRange();
if (range == cir::ComplexRangeKind::Improved ||
(range == cir::ComplexRangeKind::Promoted && !op.getPromoted()))
return buildRangeReductionComplexDiv(builder, loc, lhsReal, lhsImag,
rhsReal, rhsImag);
if (range == cir::ComplexRangeKind::Full)
return buildComplexBinOpLibCall(pass, builder, &getComplexDivLibCallName,
loc, ty, lhsReal, lhsImag, rhsReal,
rhsImag);
}
return buildAlgebraicComplexDiv(builder, loc, lhsReal, lhsImag, rhsReal,
rhsImag);
}
void LoweringPreparePass::lowerComplexBinOp(ComplexBinOp op) {
CIRBaseBuilderTy builder(getContext());
builder.setInsertionPointAfter(op);
auto loc = op.getLoc();
auto lhs = op.getLhs();
auto rhs = op.getRhs();
auto lhsReal = builder.createComplexReal(loc, lhs);
auto lhsImag = builder.createComplexImag(loc, lhs);
auto rhsReal = builder.createComplexReal(loc, rhs);
auto rhsImag = builder.createComplexImag(loc, rhs);
mlir::Value loweredResult;
if (op.getKind() == cir::ComplexBinOpKind::Mul)
loweredResult = lowerComplexMul(*this, builder, loc, op, lhsReal, lhsImag,
rhsReal, rhsImag);
else
loweredResult = lowerComplexDiv(*this, builder, loc, op, lhsReal, lhsImag,
rhsReal, rhsImag);
op.replaceAllUsesWith(loweredResult);
op.erase();
}
void LoweringPreparePass::lowerThreeWayCmpOp(CmpThreeWayOp op) {
CIRBaseBuilderTy builder(getContext());
builder.setInsertionPointAfter(op);
if (op.isIntegralComparison() && op.isStrongOrdering()) {
// For three-way comparisons on integral operands that produce strong
// ordering, we can generate potentially better code with the `llvm.scmp.*`
// and `llvm.ucmp.*` intrinsics. Thus we don't replace these comparisons
// here. They will be lowered directly to LLVMIR during the LLVM lowering
// pass.
//
// But we still need to take a step here. `llvm.scmp.*` and `llvm.ucmp.*`
// returns -1, 0, or 1 to represent lt, eq, and gt, which are the
// "canonicalized" result values of three-way comparisons. However,
// `cir.cmp3way` may not produce canonicalized result. We need to
// canonicalize the comparison if necessary. This is what we're doing in
// this special branch.
canonicalizeIntrinsicThreeWayCmp(builder, op);
return;
}
auto loc = op->getLoc();
auto cmpInfo = op.getInfo();
auto buildCmpRes = [&](int64_t value) -> mlir::Value {
return builder.create<cir::ConstantOp>(
loc, op.getType(), cir::IntAttr::get(op.getType(), value));
};
auto ltRes = buildCmpRes(cmpInfo.getLt());
auto eqRes = buildCmpRes(cmpInfo.getEq());
auto gtRes = buildCmpRes(cmpInfo.getGt());
auto buildCmp = [&](CmpOpKind kind) -> mlir::Value {
auto ty = BoolType::get(&getContext());
return builder.create<cir::CmpOp>(loc, ty, kind, op.getLhs(), op.getRhs());
};
auto buildSelect = [&](mlir::Value condition, mlir::Value trueResult,
mlir::Value falseResult) -> mlir::Value {
return builder.createSelect(loc, condition, trueResult, falseResult);
};
mlir::Value transformedResult;
if (cmpInfo.getOrdering() == CmpOrdering::Strong) {
// Strong ordering.
auto lt = buildCmp(CmpOpKind::lt);
auto eq = buildCmp(CmpOpKind::eq);
auto selectOnEq = buildSelect(eq, eqRes, gtRes);
transformedResult = buildSelect(lt, ltRes, selectOnEq);
} else {
// Partial ordering.
auto unorderedRes = buildCmpRes(cmpInfo.getUnordered().value());
auto lt = buildCmp(CmpOpKind::lt);
auto eq = buildCmp(CmpOpKind::eq);
auto gt = buildCmp(CmpOpKind::gt);
auto selectOnEq = buildSelect(eq, eqRes, unorderedRes);
auto selectOnGt = buildSelect(gt, gtRes, selectOnEq);
transformedResult = buildSelect(lt, ltRes, selectOnGt);
}
op.replaceAllUsesWith(transformedResult);
op.erase();
}
void LoweringPreparePass::lowerGlobalOp(GlobalOp op) {
auto &ctorRegion = op.getCtorRegion();
auto &dtorRegion = op.getDtorRegion();
if (!ctorRegion.empty() || !dtorRegion.empty()) {
// Build a variable initialization function and move the initialzation code
// in the ctor region over.
auto f = buildCXXGlobalVarDeclInitFunc(op);
// Clear the ctor and dtor region
ctorRegion.getBlocks().clear();
dtorRegion.getBlocks().clear();
// Add a function call to the variable initialization function.
assert(!hasAttr<clang::InitPriorityAttr>(
mlir::cast<ASTDeclInterface>(*op.getAst())) &&
"custom initialization priority NYI");
dynamicInitializers.push_back(f);
}
std::optional<mlir::ArrayAttr> annotations = op.getAnnotations();
if (annotations) {
addGlobalAnnotations(op, annotations.value());
}
}
void LoweringPreparePass::buildGlobalCtorDtorList() {
if (!globalCtorList.empty()) {
theModule->setAttr(cir::CIRDialect::getGlobalCtorsAttrName(),
mlir::ArrayAttr::get(&getContext(), globalCtorList));
}
if (!globalDtorList.empty()) {
theModule->setAttr(cir::CIRDialect::getGlobalDtorsAttrName(),
mlir::ArrayAttr::get(&getContext(), globalDtorList));
}
}
void LoweringPreparePass::buildCXXGlobalInitFunc() {
if (dynamicInitializers.empty())
return;
for (auto &f : dynamicInitializers) {
// TODO: handle globals with a user-specified initialzation priority.
auto ctorAttr = cir::GlobalCtorAttr::get(&getContext(), f.getName());
globalCtorList.push_back(ctorAttr);
}
SmallString<256> fnName;
// Include the filename in the symbol name. Including "sub_" matches gcc
// and makes sure these symbols appear lexicographically behind the symbols
// with priority emitted above. Module implementation units behave the same
// way as a non-modular TU with imports.
// TODO: check CXX20ModuleInits
if (astCtx->getCurrentNamedModule() &&
!astCtx->getCurrentNamedModule()->isModuleImplementation()) {
llvm::raw_svector_ostream Out(fnName);
std::unique_ptr<clang::MangleContext> MangleCtx(
astCtx->createMangleContext());
cast<clang::ItaniumMangleContext>(*MangleCtx)
.mangleModuleInitializer(astCtx->getCurrentNamedModule(), Out);
} else {
fnName += "_GLOBAL__sub_I_";
fnName += getTransformedFileName(theModule);
}
CIRBaseBuilderTy builder(getContext());
builder.setInsertionPointToEnd(&theModule.getBodyRegion().back());
auto fnType =
cir::FuncType::get({}, cir::VoidType::get(builder.getContext()));
FuncOp f = buildRuntimeFunction(builder, fnName, theModule.getLoc(), fnType,
cir::GlobalLinkageKind::ExternalLinkage);
builder.setInsertionPointToStart(f.addEntryBlock());
for (auto &f : dynamicInitializers) {
builder.createCallOp(f.getLoc(), f);
}
builder.create<ReturnOp>(f.getLoc());
}
void LoweringPreparePass::lowerDynamicCastOp(DynamicCastOp op) {
CIRBaseBuilderTy builder(getContext());
builder.setInsertionPointAfter(op);
assert(astCtx && "AST context is not available during lowering prepare");
auto loweredValue = cxxABI->lowerDynamicCast(builder, *astCtx, op);
op.replaceAllUsesWith(loweredValue);
op.erase();
}
static void lowerArrayDtorCtorIntoLoop(CIRBaseBuilderTy &builder,
mlir::Operation *op, mlir::Type eltTy,
mlir::Value arrayAddr,
uint64_t arrayLen) {
// Generate loop to call into ctor/dtor for every element.
auto loc = op->getLoc();
// TODO: instead of fixed integer size, create alias for PtrDiffTy and unify
// with CIRGen stuff.
auto ptrDiffTy =
cir::IntType::get(builder.getContext(), 64, /*signed=*/false);
auto numArrayElementsConst = builder.create<cir::ConstantOp>(
loc, ptrDiffTy, cir::IntAttr::get(ptrDiffTy, arrayLen));
auto begin = builder.create<cir::CastOp>(
loc, eltTy, cir::CastKind::array_to_ptrdecay, arrayAddr);
mlir::Value end = builder.create<cir::PtrStrideOp>(loc, eltTy, begin,
numArrayElementsConst);
auto tmpAddr = builder.createAlloca(
loc, /*addr type*/ builder.getPointerTo(eltTy),
/*var type*/ eltTy, "__array_idx", clang::CharUnits::One());
builder.createStore(loc, begin, tmpAddr);
auto loop = builder.createDoWhile(
loc,
/*condBuilder=*/
[&](mlir::OpBuilder &b, mlir::Location loc) {
auto currentElement = b.create<cir::LoadOp>(loc, eltTy, tmpAddr);
mlir::Type boolTy = cir::BoolType::get(b.getContext());
auto cmp = builder.create<cir::CmpOp>(loc, boolTy, cir::CmpOpKind::eq,
currentElement, end);
builder.createCondition(cmp);
},
/*bodyBuilder=*/
[&](mlir::OpBuilder &b, mlir::Location loc) {
auto currentElement = b.create<cir::LoadOp>(loc, eltTy, tmpAddr);
CallOp ctorCall;
op->walk([&](CallOp c) { ctorCall = c; });
assert(ctorCall && "expected ctor call");
auto one = builder.create<cir::ConstantOp>(
loc, ptrDiffTy, cir::IntAttr::get(ptrDiffTy, 1));
ctorCall->moveAfter(one);
ctorCall->setOperand(0, currentElement);