/
IREmitter.cc
864 lines (752 loc) · 42.3 KB
/
IREmitter.cc
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
// These violate our poisons so have to happen first
#include "llvm/IR/Attributes.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/DerivedTypes.h" // FunctionType, StructType
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Verifier.h"
#include "ast/Helpers.h"
#include "ast/ast.h"
#include "cfg/CFG.h"
#include "common/FileOps.h"
#include "common/Timer.h"
#include "common/sort.h"
#include "common/typecase.h"
#include "compiler/Core/CompilerState.h"
#include "compiler/Errors/Errors.h"
#include "compiler/IREmitter/IREmitter.h"
#include "compiler/IREmitter/IREmitterContext.h"
#include "compiler/IREmitter/IREmitterHelpers.h"
#include "compiler/IREmitter/MethodCallContext.h"
#include "compiler/IREmitter/Payload.h"
#include "compiler/Names/Names.h"
#include <string_view>
using namespace std;
namespace sorbet::compiler {
namespace {
vector<core::ArgInfo::ArgFlags> getArgFlagsForBlockId(CompilerState &cs, int blockId, core::SymbolRef method,
const IREmitterContext &irctx) {
auto ty = irctx.rubyBlockType[blockId];
ENFORCE(ty == FunctionType::Block || ty == FunctionType::Method || ty == FunctionType::StaticInitFile ||
ty == FunctionType::StaticInitModule);
if (ty == FunctionType::Block) {
auto blockLink = irctx.blockLinks[blockId];
return blockLink->argFlags;
}
vector<core::ArgInfo::ArgFlags> res;
for (auto &argInfo : method.data(cs)->arguments()) {
res.emplace_back(argInfo.flags);
}
return res;
}
void setupStackFrame(CompilerState &cs, const ast::MethodDef &md, const IREmitterContext &irctx,
llvm::IRBuilderBase &build, int rubyBlockId) {
llvm::IRBuilder<> &builder = static_cast<llvm::IRBuilder<> &>(build);
switch (irctx.rubyBlockType[rubyBlockId]) {
case FunctionType::Method:
case FunctionType::StaticInitFile:
case FunctionType::StaticInitModule:
case FunctionType::Block:
case FunctionType::Rescue:
case FunctionType::Ensure: {
// Switch the current control frame from a C frame to a Ruby-esque one
auto pc = Payload::setRubyStackFrame(cs, builder, irctx, md, rubyBlockId);
builder.CreateStore(pc, irctx.lineNumberPtrsByFunction[rubyBlockId]);
break;
}
case FunctionType::ExceptionBegin: {
// Exception functions get their pc and iseq_encoded values as arguments
auto func = irctx.rubyBlocks2Functions[rubyBlockId];
auto *pc = func->arg_begin();
builder.CreateStore(pc, irctx.lineNumberPtrsByFunction[rubyBlockId]);
break;
}
case FunctionType::Unused:
break;
}
}
} // namespace
void setupStackFrames(CompilerState &base, const ast::MethodDef &md, const IREmitterContext &irctx) {
llvm::IRBuilder<> builder(base);
for (auto rubyBlockId = 0; rubyBlockId < irctx.rubyBlocks2Functions.size(); rubyBlockId++) {
auto cs = base.withFunctionEntry(irctx.functionInitializersByFunction[rubyBlockId]);
builder.SetInsertPoint(irctx.functionInitializersByFunction[rubyBlockId]);
switch (irctx.rubyBlockType[rubyBlockId]) {
case FunctionType::Method:
case FunctionType::StaticInitFile:
case FunctionType::StaticInitModule: {
// We could get this from the frame, as below, but it is slightly more
// efficient to get it from the function arguments.
auto selfArgRaw = irctx.rubyBlocks2Functions[rubyBlockId]->arg_begin() + 2;
Payload::varSet(cs, cfg::LocalRef::selfVariable(), selfArgRaw, builder, irctx, rubyBlockId);
break;
}
case FunctionType::Block:
case FunctionType::Rescue:
case FunctionType::Ensure:
case FunctionType::ExceptionBegin: {
auto selfArgRaw = builder.CreateCall(cs.getFunction("sorbet_getSelfFromFrame"), {}, "self");
Payload::varSet(cs, cfg::LocalRef::selfVariable(), selfArgRaw, builder, irctx, rubyBlockId);
break;
}
default:
break;
}
setupStackFrame(cs, md, irctx, builder, rubyBlockId);
auto lastLoc = core::Loc::none();
auto startLoc = md.symbol.data(base)->loc();
Payload::setLineNumber(cs, builder, core::Loc(cs.file, md.loc), startLoc, lastLoc,
irctx.lineNumberPtrsByFunction[rubyBlockId]);
}
}
void setupArguments(CompilerState &base, cfg::CFG &cfg, const ast::MethodDef &md, const IREmitterContext &irctx) {
// this function effectively generate an optimized build of
// https://github.com/ruby/ruby/blob/59c3b1c9c843fcd2d30393791fe224e5789d1677/include/ruby/ruby.h#L2522-L2675
llvm::IRBuilder<> builder(base);
for (auto rubyBlockId = 0; rubyBlockId < irctx.rubyBlocks2Functions.size(); rubyBlockId++) {
auto cs = base.withFunctionEntry(irctx.functionInitializersByFunction[rubyBlockId]);
builder.SetInsertPoint(irctx.argumentSetupBlocksByFunction[rubyBlockId]);
// emit a location that corresponds to the function entry
auto loc = md.symbol.data(cs)->loc();
IREmitterHelpers::emitDebugLoc(cs, builder, irctx, rubyBlockId, loc);
auto blockType = irctx.rubyBlockType[rubyBlockId];
if (blockType == FunctionType::Method || blockType == FunctionType::StaticInitFile ||
blockType == FunctionType::StaticInitModule || blockType == FunctionType::Block) {
auto func = irctx.rubyBlocks2Functions[rubyBlockId];
auto maxPositionalArgCount = 0;
auto minPositionalArgCount = 0;
auto isBlock = blockType == FunctionType::Block;
auto hasRestArgs = false;
auto hasKWArgs = false;
auto hasKWRestArgs = false;
llvm::Value *argCountRaw = !isBlock ? func->arg_begin() : func->arg_begin() + 2;
llvm::Value *argArrayRaw = !isBlock ? func->arg_begin() + 1 : func->arg_begin() + 3;
llvm::Value *hashArgs;
cfg::LocalRef blkArgName;
cfg::LocalRef restArgName;
cfg::LocalRef kwRestArgName;
auto argsFlags = getArgFlagsForBlockId(cs, rubyBlockId, cfg.symbol, irctx);
{
auto argId = -1;
ENFORCE(argsFlags.size() == irctx.rubyBlockArgs[rubyBlockId].size());
for (auto &argFlags : argsFlags) {
argId += 1;
if (argFlags.isKeyword) {
hasKWArgs = true;
if (argFlags.isRepeated) {
kwRestArgName = irctx.rubyBlockArgs[rubyBlockId][argId];
hasKWRestArgs = true;
}
continue;
}
if (argFlags.isRepeated) {
restArgName = irctx.rubyBlockArgs[rubyBlockId][argId];
hasRestArgs = true;
continue;
}
if (argFlags.isDefault) {
maxPositionalArgCount += 1;
continue;
}
if (argFlags.isBlock) {
blkArgName = irctx.rubyBlockArgs[rubyBlockId][argId];
continue;
}
maxPositionalArgCount += 1;
minPositionalArgCount += 1;
}
}
hashArgs = Payload::rubyUndef(cs, builder);
if (hasKWArgs) {
// if last argument is a hash, it's not part of positional arguments - it's going to
// fullfill all kw arguments instead
auto hasEnoughArgs = llvm::BasicBlock::Create(cs, "readKWHashArgCountSuccess", func);
auto hasPassedHash = llvm::BasicBlock::Create(cs, "readKWHash", func);
auto afterHash = llvm::BasicBlock::Create(cs, "afterKWHash", func);
// Check that there are enough arguments to fill out minPositionalArgCount
// https://github.com/ruby/ruby/blob/59c3b1c9c843fcd2d30393791fe224e5789d1677/include/ruby/ruby.h#L2547
auto argSizeForHashCheck =
builder.CreateICmpULT(llvm::ConstantInt::get(cs, llvm::APInt(32, minPositionalArgCount)),
argCountRaw, "hashAttemptReadGuard");
builder.CreateCondBr(argSizeForHashCheck, hasEnoughArgs, afterHash);
auto sizeTestFailedEnd = builder.GetInsertBlock();
builder.SetInsertPoint(hasEnoughArgs);
llvm::Value *argsWithoutHashCount = builder.CreateSub(
argCountRaw, llvm::ConstantInt::get(cs, llvm::APInt(32, 1)), "argsWithoutHashCount");
llvm::Value *indices[] = {argsWithoutHashCount};
auto maybeHashValue = builder.CreateLoad(builder.CreateGEP(argArrayRaw, indices), "KWArgHash");
// checkIfLastArgIsHash
auto isHashValue = Payload::typeTest(cs, builder, maybeHashValue,
core::make_type<core::ClassType>(core::Symbols::Hash()));
builder.CreateCondBr(isHashValue, hasPassedHash, afterHash);
auto hashTypeFailedTestEnd = builder.GetInsertBlock();
builder.SetInsertPoint(hasPassedHash);
// yes, this is an empty block. It's used only for Phi node
auto hasPassedHashEnd = builder.GetInsertBlock();
builder.CreateBr(afterHash);
builder.SetInsertPoint(afterHash);
auto hashArgsPhi = builder.CreatePHI(builder.getInt64Ty(), 3, "hashArgsPhi");
auto argcPhi = builder.CreatePHI(builder.getInt32Ty(), 3, "argcPhi");
argcPhi->addIncoming(argCountRaw, sizeTestFailedEnd);
argcPhi->addIncoming(argCountRaw, hashTypeFailedTestEnd);
hashArgsPhi->addIncoming(hashArgs, sizeTestFailedEnd);
hashArgsPhi->addIncoming(hashArgs, hashTypeFailedTestEnd);
argcPhi->addIncoming(argsWithoutHashCount, hasPassedHashEnd);
hashArgsPhi->addIncoming(maybeHashValue, hasPassedHashEnd);
argCountRaw = argcPhi;
hashArgs = hashArgsPhi;
}
if (isBlock) {
if (minPositionalArgCount != 1) {
// blocks can expand their first argument in arg array
auto arrayTestBlock = llvm::BasicBlock::Create(cs, "argArrayExpandArrayTest", func);
auto argExpandBlock = llvm::BasicBlock::Create(cs, "argArrayExpand", func);
auto afterArgArrayExpandBlock = llvm::BasicBlock::Create(cs, "afterArgArrayExpand", func);
auto argSizeForExpansionCheck = builder.CreateICmpEQ(
argCountRaw, llvm::ConstantInt::get(cs, llvm::APInt(32, 1)), "arrayExpansionSizeGuard");
builder.CreateCondBr(argSizeForExpansionCheck, arrayTestBlock, afterArgArrayExpandBlock);
auto sizeTestEnd = builder.GetInsertBlock();
builder.SetInsertPoint(arrayTestBlock);
llvm::Value *indices[] = {llvm::ConstantInt::get(cs, llvm::APInt(32, 0, true))};
auto rawArg1Value =
builder.CreateLoad(builder.CreateGEP(argArrayRaw, indices), "arg1_maybeExpandToFullArgs");
auto isArray = Payload::typeTest(cs, builder, rawArg1Value,
core::make_type<core::ClassType>(core::Symbols::Array()));
auto typeTestEnd = builder.GetInsertBlock();
builder.CreateCondBr(isArray, argExpandBlock, afterArgArrayExpandBlock);
builder.SetInsertPoint(argExpandBlock);
auto newArgArray = builder.CreateCall(cs.getFunction("sorbet_rubyArrayInnerPtr"), {rawArg1Value},
"expandedArgArray");
auto newArgc =
builder.CreateCall(cs.getFunction("sorbet_rubyArrayLen"), {rawArg1Value}, "expandedArgc");
auto expansionEnd = builder.GetInsertBlock();
builder.CreateBr(afterArgArrayExpandBlock);
builder.SetInsertPoint(afterArgArrayExpandBlock);
auto argcPhi = builder.CreatePHI(builder.getInt32Ty(), 3, "argcPhi");
argcPhi->addIncoming(argCountRaw, sizeTestEnd);
argcPhi->addIncoming(argCountRaw, typeTestEnd);
argcPhi->addIncoming(newArgc, expansionEnd);
argCountRaw = argcPhi;
auto argArrayPhi = builder.CreatePHI(llvm::Type::getInt64PtrTy(cs), 3, "argArrayPhi");
argArrayPhi->addIncoming(argArrayRaw, sizeTestEnd);
argArrayPhi->addIncoming(argArrayRaw, typeTestEnd);
argArrayPhi->addIncoming(newArgArray, expansionEnd);
argArrayRaw = argArrayPhi;
}
minPositionalArgCount = 0;
// blocks Can have 0 args always
}
auto numOptionalArgs = maxPositionalArgCount - minPositionalArgCount;
if (!isBlock) {
// validate arg count
auto argCountFailBlock = llvm::BasicBlock::Create(cs, "argCountFailBlock", func);
auto argCountSecondCheckBlock = llvm::BasicBlock::Create(cs, "argCountSecondCheckBlock", func);
auto argCountSuccessBlock = llvm::BasicBlock::Create(cs, "argCountSuccess", func);
if (!hasRestArgs) {
auto tooManyArgs = builder.CreateICmpUGT(
argCountRaw, llvm::ConstantInt::get(cs, llvm::APInt(32, maxPositionalArgCount)), "tooManyArgs");
auto expected1 = Payload::setExpectedBool(cs, builder, tooManyArgs, false);
builder.CreateCondBr(expected1, argCountFailBlock, argCountSecondCheckBlock);
} else {
builder.CreateBr(argCountSecondCheckBlock);
}
builder.SetInsertPoint(argCountSecondCheckBlock);
auto tooFewArgs = builder.CreateICmpULT(
argCountRaw, llvm::ConstantInt::get(cs, llvm::APInt(32, minPositionalArgCount)), "tooFewArgs");
auto expected2 = Payload::setExpectedBool(cs, builder, tooFewArgs, false);
builder.CreateCondBr(expected2, argCountFailBlock, argCountSuccessBlock);
builder.SetInsertPoint(argCountFailBlock);
Payload::raiseArity(cs, builder, argCountRaw, minPositionalArgCount,
hasRestArgs ? -1 : maxPositionalArgCount);
builder.SetInsertPoint(argCountSuccessBlock);
}
vector<llvm::BasicBlock *> checkBlocks;
vector<llvm::BasicBlock *> fillFromArgBlocks;
vector<llvm::BasicBlock *> fillFromDefaultBlocks;
{
// create blocks for arg filling
for (auto i = 0; i < numOptionalArgs; i++) {
auto suffix = to_string(i);
checkBlocks.emplace_back(llvm::BasicBlock::Create(cs, {"checkBlock", suffix}, func));
fillFromDefaultBlocks.emplace_back(
llvm::BasicBlock::Create(cs, {"fillFromDefaultBlock", suffix}, func));
fillFromArgBlocks.emplace_back(llvm::BasicBlock::Create(cs, {"fillFromArgBlock", suffix}, func));
}
// create "Done" blocks (not needed for fillFromArgBlocks)
auto suffix = "Done" + to_string(numOptionalArgs);
checkBlocks.emplace_back(llvm::BasicBlock::Create(cs, {"checkBlock", suffix}, func));
fillFromDefaultBlocks.emplace_back(
llvm::BasicBlock::Create(cs, {"fillFromDefaultBlock", suffix}, func));
}
{
// fill local variables from args
auto fillRequiredArgs = llvm::BasicBlock::Create(cs, "fillRequiredArgs", func);
builder.CreateBr(fillRequiredArgs);
builder.SetInsertPoint(fillRequiredArgs);
for (auto i = 0; i < maxPositionalArgCount; i++) {
if (i >= minPositionalArgCount) {
// if these are optional, put them in their own BasicBlock
// because we might not run it
auto &block = fillFromArgBlocks[i - minPositionalArgCount];
builder.SetInsertPoint(block);
}
const auto a = irctx.rubyBlockArgs[rubyBlockId][i];
if (!a.data(cfg)._name.exists()) {
failCompilation(cs, core::Loc(cfg.file, md.declLoc),
"this method has a block argument construct that's not supported");
}
// mark the arg as present
auto &argPresent = irctx.argPresentVariables[i];
if (!isBlock && argPresent.exists()) {
Payload::varSet(cs, argPresent, Payload::rubyTrue(cs, builder), builder, irctx, rubyBlockId);
}
llvm::Value *indices[] = {llvm::ConstantInt::get(cs, llvm::APInt(32, i, true))};
auto name = a.data(cfg)._name.shortName(cs);
llvm::StringRef nameRef(name.data(), name.length());
auto rawValue = builder.CreateLoad(builder.CreateGEP(argArrayRaw, indices), {"rawArg_", nameRef});
Payload::varSet(cs, a, rawValue, builder, irctx, rubyBlockId);
if (i >= minPositionalArgCount) {
// check if we need to fill in the next variable from the arg
builder.CreateBr(checkBlocks[i - minPositionalArgCount + 1]);
}
}
// make the last instruction in all the required args point at the first check block
builder.SetInsertPoint(fillRequiredArgs);
//
if (blkArgName.exists() && irctx.usesBlockArgs) {
// TODO: I don't think this correctly handles blocks with block args
Payload::varSet(cs, blkArgName, builder.CreateCall(cs.getFunction("sorbet_getMethodBlockAsProc")),
builder, irctx, 0);
}
builder.CreateBr(checkBlocks[0]);
}
{
// build check blocks
for (auto i = 0; i < numOptionalArgs; i++) {
auto &block = checkBlocks[i];
builder.SetInsertPoint(block);
auto argCount = builder.CreateICmpEQ(
argCountRaw, llvm::ConstantInt::get(cs, llvm::APInt(32, i + minPositionalArgCount)),
llvm::Twine("default") + llvm::Twine(i));
auto expected = Payload::setExpectedBool(cs, builder, argCount, false);
builder.CreateCondBr(expected, fillFromDefaultBlocks[i], fillFromArgBlocks[i]);
}
}
{
// build fillFromDefaultBlocks
for (auto i = 0; i < numOptionalArgs; i++) {
builder.SetInsertPoint(fillFromDefaultBlocks[i]);
auto argIndex = i + minPositionalArgCount;
if (!isBlock) {
auto argPresent = irctx.argPresentVariables[argIndex];
if (argPresent.exists()) {
Payload::varSet(cs, argPresent, Payload::rubyFalse(cs, builder), builder, irctx,
rubyBlockId);
}
} else {
auto a = irctx.rubyBlockArgs[rubyBlockId][argIndex];
Payload::varSet(cs, a, Payload::rubyNil(cs, builder), builder, irctx, rubyBlockId);
}
// fall through to the next default arg init block
builder.CreateBr(fillFromDefaultBlocks[i + 1]);
}
}
{
// Tie up all the "Done" blocks at the end
builder.SetInsertPoint(checkBlocks[numOptionalArgs]);
builder.CreateBr(fillFromDefaultBlocks[numOptionalArgs]);
builder.SetInsertPoint(fillFromDefaultBlocks[numOptionalArgs]);
if (hasRestArgs) {
Payload::varSet(cs, restArgName,
Payload::readRestArgs(cs, builder, maxPositionalArgCount, argCountRaw, argArrayRaw),
builder, irctx, rubyBlockId);
}
if (hasKWArgs) {
for (int argId = maxPositionalArgCount; argId < argsFlags.size(); argId++) {
if (argsFlags[argId].isKeyword && !argsFlags[argId].isRepeated) {
auto name = irctx.rubyBlockArgs[rubyBlockId][argId];
auto rawId = Payload::idIntern(cs, builder, name.data(cfg)._name.shortName(cs));
auto rawRubySym = builder.CreateCall(cs.getFunction("rb_id2sym"), {rawId}, "rawSym");
auto argPresent = irctx.argPresentVariables[argId];
auto passedValue = Payload::getKWArg(cs, builder, hashArgs, rawRubySym);
auto isItUndef = Payload::testIsUndef(cs, builder, passedValue);
auto kwArgSet = llvm::BasicBlock::Create(cs, "kwArgSet", func);
auto kwArgDefault = llvm::BasicBlock::Create(cs, "kwArgDefault", func);
auto kwArgContinue = llvm::BasicBlock::Create(cs, "kwArgContinue", func);
builder.CreateCondBr(isItUndef, kwArgDefault, kwArgSet);
// Write a default value out, and mark the variable as missing
builder.SetInsertPoint(kwArgDefault);
if (!isBlock && argPresent.exists()) {
Payload::varSet(cs, argPresent, Payload::rubyFalse(cs, builder), builder, irctx,
rubyBlockId);
}
builder.CreateBr(kwArgContinue);
builder.SetInsertPoint(kwArgSet);
if (!isBlock && argPresent.exists()) {
Payload::varSet(cs, argPresent, Payload::rubyTrue(cs, builder), builder, irctx,
rubyBlockId);
}
Payload::varSet(cs, name, passedValue, builder, irctx, rubyBlockId);
builder.CreateBr(kwArgContinue);
builder.SetInsertPoint(kwArgContinue);
}
}
if (hasKWRestArgs) {
Payload::varSet(cs, kwRestArgName, Payload::readKWRestArg(cs, builder, hashArgs), builder,
irctx, rubyBlockId);
} else {
Payload::assertNoExtraKWArg(cs, builder, hashArgs);
}
}
}
}
switch (blockType) {
case FunctionType::Method:
case FunctionType::StaticInitFile:
case FunctionType::StaticInitModule:
case FunctionType::Block:
case FunctionType::ExceptionBegin:
case FunctionType::Rescue:
case FunctionType::Ensure:
// jump dirrectly to user body
builder.CreateBr(irctx.userEntryBlockByFunction[rubyBlockId]);
break;
case FunctionType::Unused:
// this function will never be called
builder.CreateUnreachable();
break;
}
}
}
cfg::LocalRef returnValue(cfg::CFG &cfg, CompilerState &cs) {
return cfg.enterLocal({Names::returnValue(cs), 1});
}
llvm::BasicBlock *resolveJumpTarget(cfg::CFG &cfg, const IREmitterContext &irctx, const cfg::BasicBlock *from,
const cfg::BasicBlock *to) {
if (to == cfg.deadBlock()) {
return irctx.deadBlockMapping[from->rubyBlockId];
}
auto remapped = irctx.basicBlockJumpOverrides[to->id];
if (from->rubyBlockId != irctx.basicBlockRubyBlockId[remapped]) {
return irctx.blockExitMapping[from->rubyBlockId];
} else {
return irctx.llvmBlocksBySorbetBlocks[irctx.basicBlockJumpOverrides[to->id]];
}
}
void emitUserBody(CompilerState &base, cfg::CFG &cfg, const IREmitterContext &irctx) {
llvm::IRBuilder<> builder(base);
UnorderedSet<cfg::LocalRef> loadYieldParamsResults; // methods calls on these are ignored
auto startLoc = cfg.symbol.data(base)->loc();
auto &arguments = cfg.symbol.data(base)->arguments();
for (auto it = cfg.forwardsTopoSort.rbegin(); it != cfg.forwardsTopoSort.rend(); ++it) {
cfg::BasicBlock *bb = *it;
auto cs = base.withFunctionEntry(irctx.functionInitializersByFunction[bb->rubyBlockId]);
auto block = irctx.llvmBlocksBySorbetBlocks[bb->id];
bool isTerminated = false;
builder.SetInsertPoint(block);
// NOTE: explicitly clearing debug location information here so that we don't accidentally inherit the location
// information from blocks in different target functions.
builder.SetCurrentDebugLocation(llvm::DebugLoc());
core::Loc lastLoc;
if (bb != cfg.deadBlock()) {
for (cfg::Binding &bind : bb->exprs) {
auto loc = core::Loc(cs.file, bind.loc);
lastLoc = Payload::setLineNumber(cs, builder, loc, startLoc, lastLoc,
irctx.lineNumberPtrsByFunction[bb->rubyBlockId]);
IREmitterHelpers::emitDebugLoc(cs, builder, irctx, bb->rubyBlockId, loc);
typecase(
bind.value.get(),
[&](cfg::Ident *i) {
auto var = Payload::varGet(cs, i->what, builder, irctx, bb->rubyBlockId);
Payload::varSet(cs, bind.bind.variable, var, builder, irctx, bb->rubyBlockId);
},
[&](cfg::Alias *i) {
// We compute the alias map when IREmitterContext is first created, so if an entry is missing,
// there's a problem.
ENFORCE(irctx.aliases.find(bind.bind.variable) != irctx.aliases.end(),
"Alias is missing from the alias map");
},
[&](cfg::SolveConstraint *i) {
auto var = Payload::varGet(cs, i->send, builder, irctx, bb->rubyBlockId);
Payload::varSet(cs, bind.bind.variable, var, builder, irctx, bb->rubyBlockId);
},
[&](cfg::Send *i) {
if (i->recv.variable.data(cfg)._name == core::Names::blkArg() &&
loadYieldParamsResults.contains(i->recv.variable)) {
// this loads an argument of a block.
// They are already loaded in preambula of the method
return;
}
llvm::Function *blk = nullptr;
if (i->link != nullptr) {
blk = irctx.rubyBlocks2Functions[i->link->rubyBlockId];
}
MethodCallContext mcctx{cs, builder, irctx, bb->rubyBlockId, i, blk};
auto rawCall = IREmitterHelpers::emitMethodCall(mcctx);
Payload::varSet(cs, bind.bind.variable, rawCall, builder, irctx, bb->rubyBlockId);
},
[&](cfg::Return *i) {
isTerminated = true;
auto *var = Payload::varGet(cs, i->what.variable, builder, irctx, bb->rubyBlockId);
switch (irctx.rubyBlockType[bb->rubyBlockId]) {
case FunctionType::Method:
case FunctionType::StaticInitFile:
case FunctionType::StaticInitModule: {
Payload::varSet(cs, returnValue(cfg, cs), var, builder, irctx, bb->rubyBlockId);
builder.CreateBr(irctx.postProcessBlock);
break;
}
case FunctionType::Block:
// NOTE: this doesn't catch all block-return cases:
// https://github.com/stripe/sorbet_llvm/issues/94
failCompilation(cs, core::Loc(cs.file, bind.loc),
"returns through multiple stacks not implemented");
break;
case FunctionType::ExceptionBegin:
case FunctionType::Rescue:
case FunctionType::Ensure:
case FunctionType::Unused:
IREmitterHelpers::emitReturn(cs, builder, irctx, bb->rubyBlockId, var);
break;
}
},
[&](cfg::BlockReturn *i) {
ENFORCE(bb->rubyBlockId != 0, "should never happen");
isTerminated = true;
auto var = Payload::varGet(cs, i->what.variable, builder, irctx, bb->rubyBlockId);
IREmitterHelpers::emitReturn(cs, builder, irctx, bb->rubyBlockId, var);
},
[&](cfg::LoadSelf *i) {
// it's done in function setup, no need to do anything here
},
[&](cfg::Literal *i) {
auto rawValue = IREmitterHelpers::emitLiteralish(cs, builder, i->value);
Payload::varSet(cs, bind.bind.variable, rawValue, builder, irctx, bb->rubyBlockId);
},
[&](cfg::GetCurrentException *i) {
// if this block isn't an exception block header, there's nothing to do here.
auto bodyRubyBlockId = irctx.exceptionBlockHeader[bb->id];
if (bodyRubyBlockId == 0) {
return;
}
IREmitterHelpers::emitExceptionHandlers(cs, builder, irctx, bb->rubyBlockId, bodyRubyBlockId,
bind.bind.variable);
},
[&](cfg::ArgPresent *i) {
ENFORCE(bb->rubyBlockId == 0, "ArgPresent found outside of entry-method");
// Intentionally omitted: the result of the ArgPresent call is filled out in `setupArguments`
},
[&](cfg::LoadArg *i) {
ENFORCE(bb->rubyBlockId == 0, "LoadArg found outside of entry-method");
// Argument values are loaded by `setupArguments`, we just need to check their type here
auto &argInfo = arguments[i->argId];
auto local = irctx.rubyBlockArgs[0][i->argId];
auto var = Payload::varGet(cs, local, builder, irctx, 0);
if (auto &expectedType = argInfo.type) {
if (argInfo.flags.isBlock) {
IREmitterHelpers::emitTypeTestForBlock(cs, builder, var, expectedType, "sig");
} else {
IREmitterHelpers::emitTypeTest(cs, builder, var, expectedType, "sig");
}
}
},
[&](cfg::LoadYieldParams *i) {
loadYieldParamsResults.insert(bind.bind.variable);
/* intentionally omitted, it's part of method preambula */
},
[&](cfg::Cast *i) {
auto val = Payload::varGet(cs, i->value.variable, builder, irctx, bb->rubyBlockId);
// We skip the type test for Cast instructions that assign into <self>.
// These instructions only exist in the CFG for the purpose of type checking.
// The Ruby VM already checks that self is a valid type when calling `.bind()`
// on an UnboundMethod object.
auto skipTypeTest = bind.bind.variable.data(cfg) == core::LocalVariable::selfVariable();
if (!skipTypeTest) {
IREmitterHelpers::emitTypeTest(cs, builder, val, bind.bind.type,
fmt::format("T.{}", i->cast.shortName(cs)));
}
if (i->cast == core::Names::let() || i->cast == core::Names::cast()) {
Payload::varSet(cs, bind.bind.variable, val, builder, irctx, bb->rubyBlockId);
} else if (i->cast == core::Names::assertType()) {
Payload::varSet(cs, bind.bind.variable, Payload::rubyFalse(cs, builder), builder, irctx,
bb->rubyBlockId);
}
},
[&](cfg::TAbsurd *i) {
auto val = Payload::varGet(cs, i->what.variable, builder, irctx, bb->rubyBlockId);
builder.CreateCall(cs.getFunction("sorbet_t_absurd"), {val});
});
if (isTerminated) {
break;
}
}
if (!isTerminated) {
auto *thenb = resolveJumpTarget(cfg, irctx, bb, bb->bexit.thenb);
auto *elseb = resolveJumpTarget(cfg, irctx, bb, bb->bexit.elseb);
if (thenb != elseb && bb->bexit.cond.variable != cfg::LocalRef::blockCall()) {
auto var = Payload::varGet(cs, bb->bexit.cond.variable, builder, irctx, bb->rubyBlockId);
auto condValue = Payload::testIsTruthy(cs, builder, var);
builder.CreateCondBr(condValue, thenb, elseb);
} else {
builder.CreateBr(thenb);
}
}
}
}
}
void emitDeadBlocks(CompilerState &cs, cfg::CFG &cfg, const IREmitterContext &irctx) {
llvm::IRBuilder<> builder(cs);
// Emit the dead block body for each ruby block. It should be an error to transition to the dead block, so
// we mark its body as unreachable.
for (auto rubyBlockId = 0; rubyBlockId <= cfg.maxRubyBlockId; ++rubyBlockId) {
auto *dead = irctx.deadBlockMapping[rubyBlockId];
builder.SetInsertPoint(dead);
builder.CreateUnreachable();
}
}
void emitBlockExits(CompilerState &base, cfg::CFG &cfg, const IREmitterContext &irctx) {
llvm::IRBuilder<> builder(base);
for (auto rubyBlockId = 0; rubyBlockId <= cfg.maxRubyBlockId; ++rubyBlockId) {
auto cs = base.withFunctionEntry(irctx.functionInitializersByFunction[rubyBlockId]);
builder.SetInsertPoint(irctx.blockExitMapping[rubyBlockId]);
switch (irctx.rubyBlockType[rubyBlockId]) {
case FunctionType::Method:
case FunctionType::StaticInitFile:
case FunctionType::StaticInitModule:
builder.CreateUnreachable();
break;
case FunctionType::Block:
case FunctionType::ExceptionBegin:
case FunctionType::Rescue:
case FunctionType::Ensure:
case FunctionType::Unused:
// for non-top-level functions, we return `Qundef` to indicate that this value isn't used for anything.
IREmitterHelpers::emitReturn(cs, builder, irctx, rubyBlockId, Payload::rubyUndef(cs, builder));
break;
}
}
}
void emitPostProcess(CompilerState &cs, cfg::CFG &cfg, const IREmitterContext &irctx) {
llvm::IRBuilder<> builder(cs);
builder.SetInsertPoint(irctx.postProcessBlock);
// we're only using the top-level ruby block at this point
auto rubyBlockId = 0;
auto var = Payload::varGet(cs, returnValue(cfg, cs), builder, irctx, rubyBlockId);
auto expectedType = cfg.symbol.data(cs)->resultType;
if (expectedType == nullptr) {
IREmitterHelpers::emitReturn(cs, builder, irctx, rubyBlockId, var);
return;
}
if (core::isa_type<core::ClassType>(expectedType) &&
core::cast_type_nonnull<core::ClassType>(expectedType).symbol == core::Symbols::void_()) {
auto void_ = Payload::voidSingleton(cs, builder, irctx);
IREmitterHelpers::emitReturn(cs, builder, irctx, rubyBlockId, void_);
return;
}
// sorbet-runtime doesn't check this type for abstract methods, so we won't either.
// TODO(froydnj): we should check this type.
if (!cfg.symbol.data(cs)->isAbstract()) {
IREmitterHelpers::emitTypeTest(cs, builder, var, expectedType, "Return value");
}
IREmitterHelpers::emitReturn(cs, builder, irctx, rubyBlockId, var);
}
void IREmitter::run(CompilerState &cs, cfg::CFG &cfg, const ast::MethodDef &md) {
Timer timer(cs.gs.tracer(), "IREmitter::run");
cfg::CFG::UnfreezeCFGLocalVariables unfreezeVars(cfg);
llvm::Function *func;
if (IREmitterHelpers::isClassStaticInit(cs, md.symbol)) {
func = IREmitterHelpers::getOrCreateStaticInit(cs, md.symbol, md.declLoc);
} else {
func = IREmitterHelpers::getOrCreateFunction(cs, md.symbol);
}
func = IREmitterHelpers::cleanFunctionBody(cs, func);
{
// setup function argument names
func->arg_begin()->setName("argc");
(func->arg_begin() + 1)->setName("argArray");
(func->arg_begin() + 2)->setName("selfRaw");
}
func->addFnAttr(llvm::Attribute::AttrKind::StackProtectReq);
func->addFnAttr(llvm::Attribute::AttrKind::NoUnwind);
func->addFnAttr(llvm::Attribute::AttrKind::UWTable);
llvm::IRBuilder<> builder(cs);
const IREmitterContext irctx = IREmitterHelpers::getSorbetBlocks2LLVMBlockMapping(cs, cfg, md, func);
ENFORCE(cs.functionEntryInitializers == nullptr, "modules shouldn't be reused");
setupStackFrames(cs, md, irctx);
setupArguments(cs, cfg, md, irctx);
emitUserBody(cs, cfg, irctx);
emitDeadBlocks(cs, cfg, irctx);
emitBlockExits(cs, cfg, irctx);
emitPostProcess(cs, cfg, irctx);
for (int funId = 0; funId < irctx.functionInitializersByFunction.size(); funId++) {
builder.SetInsertPoint(irctx.functionInitializersByFunction[funId]);
builder.CreateBr(irctx.argumentSetupBlocksByFunction[funId]);
}
cs.debug->finalize();
/* run verifier */
if (debug_mode && llvm::verifyFunction(*func, &llvm::errs())) {
fmt::print("failed to verify:\n");
func->dump();
ENFORCE(false);
}
cs.runCheapOptimizations(func);
}
void IREmitter::buildInitFor(CompilerState &cs, const core::SymbolRef &sym, string_view objectName) {
llvm::IRBuilder<> builder(cs);
auto owner = sym.data(cs)->owner;
auto isRoot = IREmitterHelpers::isRootishSymbol(cs, owner);
llvm::Function *entryFunc;
if (IREmitterHelpers::isFileOrClassStaticInit(cs, sym)) {
if (!isRoot) {
return;
}
// for a path like `foo/bar/baz.rb`, we want the baseName to be just `baz`, as ruby will be looking for an init
// function called `Init_baz`
auto baseName = objectName.substr(0, objectName.rfind(".rb"));
auto slash = baseName.rfind('/');
if (slash != string_view::npos) {
baseName.remove_prefix(slash + 1);
}
auto linkageType = llvm::Function::ExternalLinkage;
std::vector<llvm::Type *> NoArgs(0, llvm::Type::getVoidTy(cs));
auto ft = llvm::FunctionType::get(llvm::Type::getVoidTy(cs), NoArgs, false);
entryFunc = llvm::Function::Create(ft, linkageType, "Init_" + (string)baseName, *cs.module);
} else {
entryFunc = IREmitterHelpers::getInitFunction(cs, sym);
}
auto bb = llvm::BasicBlock::Create(cs, "entry", entryFunc);
builder.SetInsertPoint(bb);
if (IREmitterHelpers::isFileOrClassStaticInit(cs, sym)) {
// We include sorbet_version.c when compiling sorbet_llvm itself to get the expected version.
// The actual version will be linked into libruby.so and compared against at runtime.
auto compileTimeBuildSCMRevision = sorbet_getBuildSCMRevision();
auto compileTimeIsReleaseBuild = sorbet_getIsReleaseBuild();
builder.CreateCall(cs.getFunction("sorbet_ensureSorbetRuby"),
{
llvm::ConstantInt::get(cs, llvm::APInt(32, compileTimeIsReleaseBuild, true)),
Payload::toCString(cs, compileTimeBuildSCMRevision, builder),
});
auto realpath = builder.CreateCall(cs.getFunction("sorbet_readRealpath"), {});
realpath->setName("realpath");
builder.CreateCall(cs.getFunction("sorbet_globalConstructors"), {realpath});
core::SymbolRef staticInit = cs.gs.lookupStaticInitForFile(sym.data(cs)->loc());
// Call the LLVM method that was made by run() from this Init_ method
auto staticInitName = IREmitterHelpers::getFunctionName(cs, staticInit);
auto staticInitFunc = cs.getFunction(staticInitName);
ENFORCE(staticInitFunc, staticInitName + " does not exist");
builder.CreateCall(staticInitFunc,
{
llvm::ConstantInt::get(cs, llvm::APInt(32, 0, true)),
llvm::ConstantPointerNull::get(llvm::Type::getInt64PtrTy(cs)),
Payload::rubyTopSelf(cs, builder),
},
staticInitName);
}
builder.CreateRetVoid();
if (debug_mode && llvm::verifyFunction(*entryFunc, &llvm::errs())) {
fmt::print("failed to verify:\n");
entryFunc->dump();
ENFORCE(false);
}
cs.runCheapOptimizations(entryFunc);
}
} // namespace sorbet::compiler