-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslang_backend.cpp
More file actions
1153 lines (971 loc) · 40.2 KB
/
slang_backend.cpp
File metadata and controls
1153 lines (971 loc) · 40.2 KB
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
/*
* Copyright 2010-2012, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "slang_backend.h"
#include <string>
#include <vector>
#include <iostream>
#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclGroup.h"
#include "clang/AST/RecordLayout.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/CodeGen/ModuleBuilder.h"
#include "clang/Frontend/CodeGenOptions.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "llvm/ADT/Twine.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/CodeGen/RegAllocRegistry.h"
#include "llvm/CodeGen/SchedulerRegistry.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/IRPrintingPasses.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/MC/SubtargetFeature.h"
#include "slang_assert.h"
#include "slang.h"
#include "slang_bitcode_gen.h"
#include "slang_rs_context.h"
#include "slang_rs_export_foreach.h"
#include "slang_rs_export_func.h"
#include "slang_rs_export_reduce.h"
#include "slang_rs_export_type.h"
#include "slang_rs_export_var.h"
#include "slang_rs_metadata.h"
#include "rs_cc_options.h"
#include "strip_unknown_attributes.h"
namespace slang {
void Backend::CreateFunctionPasses() {
if (!mPerFunctionPasses) {
mPerFunctionPasses = new llvm::legacy::FunctionPassManager(mpModule);
llvm::PassManagerBuilder PMBuilder;
PMBuilder.OptLevel = mCodeGenOpts.OptimizationLevel;
PMBuilder.populateFunctionPassManager(*mPerFunctionPasses);
}
}
void Backend::CreateModulePasses() {
if (!mPerModulePasses) {
mPerModulePasses = new llvm::legacy::PassManager();
llvm::PassManagerBuilder PMBuilder;
PMBuilder.OptLevel = mCodeGenOpts.OptimizationLevel;
PMBuilder.SizeLevel = mCodeGenOpts.OptimizeSize;
PMBuilder.DisableUnitAtATime = 0; // TODO Pirama confirm if this is right
if (mCodeGenOpts.UnrollLoops) {
PMBuilder.DisableUnrollLoops = 0;
} else {
PMBuilder.DisableUnrollLoops = 1;
}
PMBuilder.populateModulePassManager(*mPerModulePasses);
// Add a pass to strip off unknown/unsupported attributes.
mPerModulePasses->add(createStripUnknownAttributesPass());
}
}
bool Backend::CreateCodeGenPasses() {
if ((mOT != Slang::OT_Assembly) && (mOT != Slang::OT_Object))
return true;
// Now we add passes for code emitting
if (mCodeGenPasses) {
return true;
} else {
mCodeGenPasses = new llvm::legacy::FunctionPassManager(mpModule);
}
// Create the TargetMachine for generating code.
std::string Triple = mpModule->getTargetTriple();
std::string Error;
const llvm::Target* TargetInfo =
llvm::TargetRegistry::lookupTarget(Triple, Error);
if (TargetInfo == nullptr) {
mDiagEngine.Report(clang::diag::err_fe_unable_to_create_target) << Error;
return false;
}
// Target Machine Options
llvm::TargetOptions Options;
// Use soft-float ABI for ARM (which is the target used by Slang during code
// generation). Codegen still uses hardware FPU by default. To use software
// floating point, add 'soft-float' feature to FeaturesStr below.
Options.FloatABIType = llvm::FloatABI::Soft;
// BCC needs all unknown symbols resolved at compilation time. So we don't
// need any relocation model.
llvm::Reloc::Model RM = llvm::Reloc::Static;
// This is set for the linker (specify how large of the virtual addresses we
// can access for all unknown symbols.)
llvm::CodeModel::Model CM;
if (mpModule->getDataLayout().getPointerSize() == 4) {
CM = llvm::CodeModel::Small;
} else {
// The target may have pointer size greater than 32 (e.g. x86_64
// architecture) may need large data address model
CM = llvm::CodeModel::Medium;
}
// Setup feature string
std::string FeaturesStr;
if (mTargetOpts.CPU.size() || mTargetOpts.Features.size()) {
llvm::SubtargetFeatures Features;
for (std::vector<std::string>::const_iterator
I = mTargetOpts.Features.begin(), E = mTargetOpts.Features.end();
I != E;
I++)
Features.AddFeature(*I);
FeaturesStr = Features.getString();
}
llvm::TargetMachine *TM =
TargetInfo->createTargetMachine(Triple, mTargetOpts.CPU, FeaturesStr,
Options, RM, CM);
// Register allocation policy:
// createFastRegisterAllocator: fast but bad quality
// createGreedyRegisterAllocator: not so fast but good quality
llvm::RegisterRegAlloc::setDefault((mCodeGenOpts.OptimizationLevel == 0) ?
llvm::createFastRegisterAllocator :
llvm::createGreedyRegisterAllocator);
llvm::CodeGenOpt::Level OptLevel = llvm::CodeGenOpt::Default;
if (mCodeGenOpts.OptimizationLevel == 0) {
OptLevel = llvm::CodeGenOpt::None;
} else if (mCodeGenOpts.OptimizationLevel == 3) {
OptLevel = llvm::CodeGenOpt::Aggressive;
}
llvm::TargetMachine::CodeGenFileType CGFT =
llvm::TargetMachine::CGFT_AssemblyFile;
if (mOT == Slang::OT_Object) {
CGFT = llvm::TargetMachine::CGFT_ObjectFile;
}
if (TM->addPassesToEmitFile(*mCodeGenPasses, mBufferOutStream,
CGFT, OptLevel)) {
mDiagEngine.Report(clang::diag::err_fe_unable_to_interface_with_target);
return false;
}
return true;
}
Backend::Backend(RSContext *Context, clang::DiagnosticsEngine *DiagEngine,
const RSCCOptions &Opts,
const clang::HeaderSearchOptions &HeaderSearchOpts,
const clang::PreprocessorOptions &PreprocessorOpts,
const clang::CodeGenOptions &CodeGenOpts,
const clang::TargetOptions &TargetOpts, PragmaList *Pragmas,
llvm::raw_ostream *OS, Slang::OutputType OT,
clang::SourceManager &SourceMgr, bool AllowRSPrefix,
bool IsFilterscript)
: ASTConsumer(), mTargetOpts(TargetOpts), mpModule(nullptr), mpOS(OS),
mOT(OT), mGen(nullptr), mPerFunctionPasses(nullptr),
mPerModulePasses(nullptr), mCodeGenPasses(nullptr),
mBufferOutStream(*mpOS), mContext(Context),
mSourceMgr(SourceMgr), mASTPrint(Opts.mASTPrint), mAllowRSPrefix(AllowRSPrefix),
mIsFilterscript(IsFilterscript), mExportVarMetadata(nullptr),
mExportFuncMetadata(nullptr), mExportForEachNameMetadata(nullptr),
mExportForEachSignatureMetadata(nullptr),
mExportReduceMetadata(nullptr),
mExportTypeMetadata(nullptr), mRSObjectSlotsMetadata(nullptr),
mRefCount(mContext->getASTContext()),
mASTChecker(Context, Context->getTargetAPI(), IsFilterscript),
mForEachHandler(Context),
mLLVMContext(slang::getGlobalLLVMContext()), mDiagEngine(*DiagEngine),
mCodeGenOpts(CodeGenOpts), mPragmas(Pragmas) {
mGen = CreateLLVMCodeGen(mDiagEngine, "", HeaderSearchOpts, PreprocessorOpts,
mCodeGenOpts, mLLVMContext);
}
void Backend::Initialize(clang::ASTContext &Ctx) {
mGen->Initialize(Ctx);
mpModule = mGen->GetModule();
}
void Backend::HandleTranslationUnit(clang::ASTContext &Ctx) {
HandleTranslationUnitPre(Ctx);
if (mASTPrint)
Ctx.getTranslationUnitDecl()->dump();
mGen->HandleTranslationUnit(Ctx);
// Here, we complete a translation unit (whole translation unit is now in LLVM
// IR). Now, interact with LLVM backend to generate actual machine code (asm
// or machine code, whatever.)
// Silently ignore if we weren't initialized for some reason.
if (!mpModule)
return;
llvm::Module *M = mGen->ReleaseModule();
if (!M) {
// The module has been released by IR gen on failures, do not double free.
mpModule = nullptr;
return;
}
slangAssert(mpModule == M &&
"Unexpected module change during LLVM IR generation");
// Insert #pragma information into metadata section of module
if (!mPragmas->empty()) {
llvm::NamedMDNode *PragmaMetadata =
mpModule->getOrInsertNamedMetadata(Slang::PragmaMetadataName);
for (PragmaList::const_iterator I = mPragmas->begin(), E = mPragmas->end();
I != E;
I++) {
llvm::SmallVector<llvm::Metadata*, 2> Pragma;
// Name goes first
Pragma.push_back(llvm::MDString::get(mLLVMContext, I->first));
// And then value
Pragma.push_back(llvm::MDString::get(mLLVMContext, I->second));
// Create MDNode and insert into PragmaMetadata
PragmaMetadata->addOperand(
llvm::MDNode::get(mLLVMContext, Pragma));
}
}
HandleTranslationUnitPost(mpModule);
// Create passes for optimization and code emission
// Create and run per-function passes
CreateFunctionPasses();
if (mPerFunctionPasses) {
mPerFunctionPasses->doInitialization();
for (llvm::Module::iterator I = mpModule->begin(), E = mpModule->end();
I != E;
I++)
if (!I->isDeclaration())
mPerFunctionPasses->run(*I);
mPerFunctionPasses->doFinalization();
}
// Create and run module passes
CreateModulePasses();
if (mPerModulePasses)
mPerModulePasses->run(*mpModule);
switch (mOT) {
case Slang::OT_Assembly:
case Slang::OT_Object: {
if (!CreateCodeGenPasses())
return;
mCodeGenPasses->doInitialization();
for (llvm::Module::iterator I = mpModule->begin(), E = mpModule->end();
I != E;
I++)
if (!I->isDeclaration())
mCodeGenPasses->run(*I);
mCodeGenPasses->doFinalization();
break;
}
case Slang::OT_LLVMAssembly: {
llvm::legacy::PassManager *LLEmitPM = new llvm::legacy::PassManager();
LLEmitPM->add(llvm::createPrintModulePass(mBufferOutStream));
LLEmitPM->run(*mpModule);
break;
}
case Slang::OT_Bitcode: {
writeBitcode(mBufferOutStream, *mpModule, getTargetAPI(),
mCodeGenOpts.OptimizationLevel, mCodeGenOpts.getDebugInfo());
break;
}
case Slang::OT_Nothing: {
return;
}
default: {
slangAssert(false && "Unknown output type");
}
}
}
// Insert explicit padding fields into struct to follow the current layout.
//
// A similar algorithm is present in PadHelperFunctionStruct().
void Backend::PadStruct(clang::RecordDecl* RD) {
// Example of padding:
//
// // ORIGINAL CODE // TRANSFORMED CODE
// struct foo { struct foo {
// int a; int a;
// // 4 bytes of padding char <RS_PADDING_FIELD_NAME>[4];
// long b; long b;
// int c; int c;
// // 4 bytes of (tail) padding char <RS_PADDING_FIELD_NAME>[4];
// }; };
// We collect all of RD's fields in a vector FieldsInfo. We
// represent tail padding as an entry in the FieldsInfo vector with a
// null FieldDecl.
typedef std::pair<size_t, clang::FieldDecl*> FieldInfoType; // (pre-field padding bytes, field)
std::vector<FieldInfoType> FieldsInfo;
// RenderScript is C99-based, so we only expect to see fields. We
// could iterate over fields, but instead let's iterate over
// everything, to verify that there are only fields.
for (clang::Decl* D : RD->decls()) {
clang::FieldDecl* FD = clang::dyn_cast<clang::FieldDecl>(D);
slangAssert(FD && "found a non field declaration within a struct");
FieldsInfo.push_back(std::make_pair(size_t(0), FD));
}
clang::ASTContext& ASTC = mContext->getASTContext();
// ASTContext caches record layout. We may transform the record in a way
// that would render this cached information incorrect. clang does
// not provide any way to invalidate this cached information. We
// take the following approach:
//
// 1. ASSUME that record layout has not yet been computed for RD.
//
// 2. Create a temporary clone of RD, and compute its layout.
// ASSUME that we know how to clone RD in a way that copies all the
// properties that are relevant to its layout.
//
// 3. Use the layout information from the temporary clone to
// transform RD.
//
// NOTE: ASTContext also caches TypeInfo (see
// ASTContext::getTypeInfo()). ASSUME that inserting padding
// fields doesn't change the type in any way that affects
// TypeInfo.
//
// NOTE: A RecordType knows its associated RecordDecl -- so even
// while we're manipulating RD, the associated RecordType
// still recognizes RD as its RecordDecl. ASSUME that we
// don't do anything during our manipulation that would cause
// the RecordType to be followed to RD while RD is in a
// partially transformed state.
// The assumptions above may be brittle, and if they are incorrect,
// we may get mysterious failures.
// create a temporary clone
clang::RecordDecl* RDForLayout =
clang::RecordDecl::Create(ASTC, clang::TTK_Struct, RD->getDeclContext(),
clang::SourceLocation(), clang::SourceLocation(),
nullptr /* IdentifierInfo */);
RDForLayout->startDefinition();
RDForLayout->setTypeForDecl(RD->getTypeForDecl());
RDForLayout->setAttrs(RD->getAttrs());
RDForLayout->completeDefinition();
// move all fields from RD to RDForLayout
for (const auto &info : FieldsInfo) {
RD->removeDecl(info.second);
RDForLayout->addDecl(info.second);
}
const clang::ASTRecordLayout& RL = ASTC.getASTRecordLayout(RDForLayout);
// An exportable type cannot contain a bitfield. However, it's
// possible that this current type might have a bitfield and yet
// share a common initial sequence with an exportable type, so even
// if the current type has a bitfield, the current type still
// needs to have explicit padding inserted (in case the two types
// under discussion are members of a union). We don't need to
// insert any padding after the bitfield, however, because that
// would be beyond the common initial sequence.
bool foundBitField = false;
// Is there any padding in this struct?
bool foundPadding = false;
unsigned fieldNo = 0;
uint64_t fieldPrePaddingOffset = 0; // byte offset of pre-field padding within struct
for (auto &info : FieldsInfo) {
const clang::FieldDecl* FD = info.second;
if ((foundBitField = FD->isBitField()))
break;
const uint64_t fieldOffset = RL.getFieldOffset(fieldNo) >> 3;
const size_t prePadding = fieldOffset - fieldPrePaddingOffset;
foundPadding |= (prePadding != 0);
info.first = prePadding;
// get ready for the next field
//
// assumes that getTypeSize() is the storage size of the Type -- for example,
// that it includes a struct's tail padding (if any)
//
fieldPrePaddingOffset = fieldOffset + (ASTC.getTypeSize(FD->getType()) >> 3);
++fieldNo;
}
if (!foundBitField) {
if (const size_t tailPadding = RL.getSize().getQuantity() - fieldPrePaddingOffset) {
foundPadding = true;
FieldsInfo.push_back(std::make_pair(tailPadding, nullptr));
}
}
if (false /* change to "true" for extra debugging output */) {
if (foundPadding) {
std::cout << "PadStruct(" << RD->getNameAsString() << "):" << std::endl;
for (const auto &info : FieldsInfo)
std::cout << " " << info.first << ", " << (info.second ? info.second->getNameAsString() : "<tail>") << std::endl;
}
}
if (foundPadding && Slang::IsLocInRSHeaderFile(RD->getLocation(), mSourceMgr)) {
mContext->ReportError(RD->getLocation(), "system structure contains padding: '%0'")
<< RD->getName();
}
// now move fields from RDForLayout to RD, and add any necessary
// padding fields
const clang::QualType byteType = ASTC.getIntTypeForBitwidth(8, false /* not signed */);
clang::IdentifierInfo* const paddingIdentifierInfo = &ASTC.Idents.get(RS_PADDING_FIELD_NAME);
for (const auto &info : FieldsInfo) {
if (info.first != 0) {
// Create a padding field: "char <RS_PADDING_FIELD_NAME>[<info.first>];"
// TODO: Do we need to do anything else to keep this field from being shown in debugger?
// There's no source location, and the field is marked as implicit.
const clang::QualType paddingType =
ASTC.getConstantArrayType(byteType,
llvm::APInt(sizeof(info.first) << 3, info.first),
clang::ArrayType::Normal, 0 /* IndexTypeQuals */);
clang::FieldDecl* const FD =
clang::FieldDecl::Create(ASTC, RD, clang::SourceLocation(), clang::SourceLocation(),
paddingIdentifierInfo,
paddingType,
nullptr, // TypeSourceInfo*
nullptr, // BW (bitwidth)
false, // Mutable = false
clang::ICIS_NoInit);
FD->setImplicit(true);
RD->addDecl(FD);
}
if (info.second != nullptr) {
RDForLayout->removeDecl(info.second);
RD->addDecl(info.second);
}
}
// There does not appear to be any safe way to delete a RecordDecl
// -- for example, there is no RecordDecl destructor to invalidate
// cached record layout, and if we were to get unlucky, some future
// RecordDecl could be allocated in the same place as a deleted
// RDForLayout and "inherit" the cached record layout from
// RDForLayout.
}
void Backend::HandleTagDeclDefinition(clang::TagDecl *D) {
// we want to insert explicit padding fields into structs per http://b/29154200 and http://b/28070272
switch (D->getTagKind()) {
case clang::TTK_Struct:
PadStruct(llvm::cast<clang::RecordDecl>(D));
break;
case clang::TTK_Union:
// cannot be part of an exported type
break;
case clang::TTK_Enum:
// a scalar
break;
case clang::TTK_Class:
case clang::TTK_Interface:
default:
slangAssert(false && "Unexpected TagTypeKind");
break;
}
mGen->HandleTagDeclDefinition(D);
}
void Backend::CompleteTentativeDefinition(clang::VarDecl *D) {
mGen->CompleteTentativeDefinition(D);
}
Backend::~Backend() {
delete mpModule;
delete mGen;
delete mPerFunctionPasses;
delete mPerModulePasses;
delete mCodeGenPasses;
}
// 1) Add zero initialization of local RS object types
void Backend::AnnotateFunction(clang::FunctionDecl *FD) {
if (FD &&
FD->hasBody() &&
!Slang::IsLocInRSHeaderFile(FD->getLocation(), mSourceMgr)) {
mRefCount.Init();
mRefCount.SetDeclContext(FD);
mRefCount.Visit(FD->getBody());
}
}
bool Backend::HandleTopLevelDecl(clang::DeclGroupRef D) {
// Find and remember the types for rs_allocation and rs_script_call_t so
// they can be used later for translating rsForEach() calls.
for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end();
(mContext->getAllocationType().isNull() ||
mContext->getScriptCallType().isNull()) &&
I != E; I++) {
if (clang::TypeDecl* TD = llvm::dyn_cast<clang::TypeDecl>(*I)) {
clang::StringRef TypeName = TD->getName();
if (TypeName.equals("rs_allocation")) {
mContext->setAllocationType(TD);
} else if (TypeName.equals("rs_script_call_t")) {
mContext->setScriptCallType(TD);
}
}
}
// Disallow user-defined functions with prefix "rs"
if (!mAllowRSPrefix) {
// Iterate all function declarations in the program.
for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end();
I != E; I++) {
clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I);
if (FD == nullptr)
continue;
if (!FD->getName().startswith("rs")) // Check prefix
continue;
if (!Slang::IsLocInRSHeaderFile(FD->getLocation(), mSourceMgr))
mContext->ReportError(FD->getLocation(),
"invalid function name prefix, "
"\"rs\" is reserved: '%0'")
<< FD->getName();
}
}
for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; I++) {
clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I);
if (FD) {
// Handle forward reference from pragma (see
// RSReducePragmaHandler::HandlePragma for backward reference).
mContext->markUsedByReducePragma(FD, RSContext::CheckNameYes);
if (FD->isGlobal()) {
// Check that we don't have any array parameters being misinterpreted as
// kernel pointers due to the C type system's array to pointer decay.
size_t numParams = FD->getNumParams();
for (size_t i = 0; i < numParams; i++) {
const clang::ParmVarDecl *PVD = FD->getParamDecl(i);
clang::QualType QT = PVD->getOriginalType();
if (QT->isArrayType()) {
mContext->ReportError(
PVD->getTypeSpecStartLoc(),
"exported function parameters may not have array type: %0")
<< QT;
}
}
AnnotateFunction(FD);
}
}
if (getTargetAPI() >= SLANG_FEATURE_SINGLE_SOURCE_API) {
if (FD && FD->hasBody() &&
!Slang::IsLocInRSHeaderFile(FD->getLocation(), mSourceMgr)) {
if (FD->hasAttr<clang::RenderScriptKernelAttr>()) {
// Log functions with attribute "kernel" by their names, and assign
// them slot numbers. Any other function cannot be used in a
// rsForEach() or rsForEachWithOptions() call, including old-style
// kernel functions which are defined without the "kernel" attribute.
mContext->addForEach(FD);
}
// Look for any kernel launch calls and translate them into using the
// internal API.
// Report a compiler error on kernel launches inside a kernel.
mForEachHandler.handleForEachCalls(FD, getTargetAPI());
}
}
}
return mGen->HandleTopLevelDecl(D);
}
void Backend::HandleTranslationUnitPre(clang::ASTContext &C) {
clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
if (!mContext->processReducePragmas(this))
return;
// If we have an invalid RS/FS AST, don't check further.
if (!mASTChecker.Validate()) {
return;
}
if (mIsFilterscript) {
mContext->addPragma("rs_fp_relaxed", "");
}
int version = mContext->getVersion();
if (version == 0) {
// Not setting a version is an error
mDiagEngine.Report(
mSourceMgr.getLocForEndOfFile(mSourceMgr.getMainFileID()),
mDiagEngine.getCustomDiagID(
clang::DiagnosticsEngine::Error,
"missing pragma for version in source file"));
} else {
slangAssert(version == 1);
}
if (mContext->getReflectJavaPackageName().empty()) {
mDiagEngine.Report(
mSourceMgr.getLocForEndOfFile(mSourceMgr.getMainFileID()),
mDiagEngine.getCustomDiagID(clang::DiagnosticsEngine::Error,
"missing \"#pragma rs "
"java_package_name(com.foo.bar)\" "
"in source file"));
return;
}
// Create a static global destructor if necessary (to handle RS object
// runtime cleanup).
clang::FunctionDecl *FD = mRefCount.CreateStaticGlobalDtor();
if (FD) {
HandleTopLevelDecl(clang::DeclGroupRef(FD));
}
// Process any static function declarations
for (clang::DeclContext::decl_iterator I = TUDecl->decls_begin(),
E = TUDecl->decls_end(); I != E; I++) {
if ((I->getKind() >= clang::Decl::firstFunction) &&
(I->getKind() <= clang::Decl::lastFunction)) {
clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I);
if (FD && !FD->isGlobal()) {
AnnotateFunction(FD);
}
}
}
}
///////////////////////////////////////////////////////////////////////////////
void Backend::dumpExportVarInfo(llvm::Module *M) {
int slotCount = 0;
if (mExportVarMetadata == nullptr)
mExportVarMetadata = M->getOrInsertNamedMetadata(RS_EXPORT_VAR_MN);
llvm::SmallVector<llvm::Metadata *, 2> ExportVarInfo;
// We emit slot information (#rs_object_slots) for any reference counted
// RS type or pointer (which can also be bound).
for (RSContext::const_export_var_iterator I = mContext->export_vars_begin(),
E = mContext->export_vars_end();
I != E;
I++) {
const RSExportVar *EV = *I;
const RSExportType *ET = EV->getType();
bool countsAsRSObject = false;
// Variable name
ExportVarInfo.push_back(
llvm::MDString::get(mLLVMContext, EV->getName().c_str()));
// Type name
switch (ET->getClass()) {
case RSExportType::ExportClassPrimitive: {
const RSExportPrimitiveType *PT =
static_cast<const RSExportPrimitiveType*>(ET);
ExportVarInfo.push_back(
llvm::MDString::get(
mLLVMContext, llvm::utostr(PT->getType())));
if (PT->isRSObjectType()) {
countsAsRSObject = true;
}
break;
}
case RSExportType::ExportClassPointer: {
ExportVarInfo.push_back(
llvm::MDString::get(
mLLVMContext, ("*" + static_cast<const RSExportPointerType*>(ET)
->getPointeeType()->getName()).c_str()));
break;
}
case RSExportType::ExportClassMatrix: {
ExportVarInfo.push_back(
llvm::MDString::get(
mLLVMContext, llvm::utostr(
/* TODO Strange value. This pushes just a number, quite
* different than the other cases. What is this used for?
* These are the metadata values that some partner drivers
* want to reference (for TBAA, etc.). We may want to look
* at whether these provide any reasonable value (or have
* distinct enough values to actually depend on).
*/
DataTypeRSMatrix2x2 +
static_cast<const RSExportMatrixType*>(ET)->getDim() - 2)));
break;
}
case RSExportType::ExportClassVector:
case RSExportType::ExportClassConstantArray:
case RSExportType::ExportClassRecord: {
ExportVarInfo.push_back(
llvm::MDString::get(mLLVMContext,
EV->getType()->getName().c_str()));
break;
}
}
mExportVarMetadata->addOperand(
llvm::MDNode::get(mLLVMContext, ExportVarInfo));
ExportVarInfo.clear();
if (mRSObjectSlotsMetadata == nullptr) {
mRSObjectSlotsMetadata =
M->getOrInsertNamedMetadata(RS_OBJECT_SLOTS_MN);
}
if (countsAsRSObject) {
mRSObjectSlotsMetadata->addOperand(llvm::MDNode::get(mLLVMContext,
llvm::MDString::get(mLLVMContext, llvm::utostr(slotCount))));
}
slotCount++;
}
}
// A similar algorithm is present in Backend::PadStruct().
static void PadHelperFunctionStruct(llvm::Module *M,
llvm::StructType **paddedStructType,
std::vector<unsigned> *origFieldNumToPaddedFieldNum,
llvm::StructType *origStructType) {
slangAssert(origFieldNumToPaddedFieldNum->empty());
origFieldNumToPaddedFieldNum->reserve(2 * origStructType->getNumElements());
llvm::LLVMContext &llvmContext = M->getContext();
const llvm::DataLayout *DL = &M->getDataLayout();
const llvm::StructLayout *SL = DL->getStructLayout(origStructType);
// Field types -- including any padding fields we need to insert.
std::vector<llvm::Type *> paddedFieldTypes;
paddedFieldTypes.reserve(2 * origStructType->getNumElements());
// Is there any padding in this struct?
bool foundPadding = false;
llvm::Type *const byteType = llvm::Type::getInt8Ty(llvmContext);
unsigned origFieldNum = 0, paddedFieldNum = 0;
uint64_t fieldPrePaddingOffset = 0; // byte offset of pre-field padding within struct
for (llvm::Type *fieldType : origStructType->elements()) {
const uint64_t fieldOffset = SL->getElementOffset(origFieldNum);
const size_t prePadding = fieldOffset - fieldPrePaddingOffset;
if (prePadding != 0) {
foundPadding = true;
paddedFieldTypes.push_back(llvm::ArrayType::get(byteType, prePadding));
++paddedFieldNum;
}
paddedFieldTypes.push_back(fieldType);
(*origFieldNumToPaddedFieldNum)[origFieldNum] = paddedFieldNum;
// get ready for the next field
fieldPrePaddingOffset = fieldOffset + DL->getTypeAllocSize(fieldType);
++origFieldNum;
++paddedFieldNum;
}
*paddedStructType = (foundPadding
? llvm::StructType::get(llvmContext, paddedFieldTypes)
: origStructType);
}
void Backend::dumpExportFunctionInfo(llvm::Module *M) {
if (mExportFuncMetadata == nullptr)
mExportFuncMetadata =
M->getOrInsertNamedMetadata(RS_EXPORT_FUNC_MN);
llvm::SmallVector<llvm::Metadata *, 1> ExportFuncInfo;
for (RSContext::const_export_func_iterator
I = mContext->export_funcs_begin(),
E = mContext->export_funcs_end();
I != E;
I++) {
const RSExportFunc *EF = *I;
// Function name
if (!EF->hasParam()) {
ExportFuncInfo.push_back(llvm::MDString::get(mLLVMContext,
EF->getName().c_str()));
} else {
llvm::Function *F = M->getFunction(EF->getName());
llvm::Function *HelperFunction;
const std::string HelperFunctionName(".helper_" + EF->getName());
slangAssert(F && "Function marked as exported disappeared in Bitcode");
// Create helper function
{
llvm::StructType *OrigHelperFunctionParameterTy = nullptr;
llvm::StructType *PaddedHelperFunctionParameterTy = nullptr;
std::vector<unsigned> OrigFieldNumToPaddedFieldNum;
std::vector<bool> isStructInput;
if (!F->getArgumentList().empty()) {
std::vector<llvm::Type*> HelperFunctionParameterTys;
for (llvm::Function::arg_iterator AI = F->arg_begin(),
AE = F->arg_end(); AI != AE; AI++) {
if (AI->getType()->isPointerTy() && AI->getType()->getPointerElementType()->isStructTy()) {
HelperFunctionParameterTys.push_back(AI->getType()->getPointerElementType());
isStructInput.push_back(true);
} else {
HelperFunctionParameterTys.push_back(AI->getType());
isStructInput.push_back(false);
}
}
OrigHelperFunctionParameterTy =
llvm::StructType::get(mLLVMContext, HelperFunctionParameterTys);
PadHelperFunctionStruct(M,
&PaddedHelperFunctionParameterTy, &OrigFieldNumToPaddedFieldNum,
OrigHelperFunctionParameterTy);
}
if (!EF->checkParameterPacketType(OrigHelperFunctionParameterTy)) {
fprintf(stderr, "Failed to export function %s: parameter type "
"mismatch during creation of helper function.\n",
EF->getName().c_str());
const RSExportRecordType *Expected = EF->getParamPacketType();
if (Expected) {
fprintf(stderr, "Expected:\n");
Expected->getLLVMType()->dump();
}
if (OrigHelperFunctionParameterTy) {
fprintf(stderr, "Got:\n");
OrigHelperFunctionParameterTy->dump();
}
}
std::vector<llvm::Type*> Params;
if (PaddedHelperFunctionParameterTy) {
llvm::PointerType *HelperFunctionParameterTyP =
llvm::PointerType::getUnqual(PaddedHelperFunctionParameterTy);
Params.push_back(HelperFunctionParameterTyP);
}
llvm::FunctionType * HelperFunctionType =
llvm::FunctionType::get(F->getReturnType(),
Params,
/* IsVarArgs = */false);
HelperFunction =
llvm::Function::Create(HelperFunctionType,
llvm::GlobalValue::ExternalLinkage,
HelperFunctionName,
M);
HelperFunction->addFnAttr(llvm::Attribute::NoInline);
HelperFunction->setCallingConv(F->getCallingConv());
// Create helper function body
{
llvm::Argument *HelperFunctionParameter =
&(*HelperFunction->arg_begin());
llvm::BasicBlock *BB =
llvm::BasicBlock::Create(mLLVMContext, "entry", HelperFunction);
llvm::IRBuilder<> *IB = new llvm::IRBuilder<>(BB);
llvm::SmallVector<llvm::Value*, 6> Params;
llvm::Value *Idx[2];
Idx[0] =
llvm::ConstantInt::get(llvm::Type::getInt32Ty(mLLVMContext), 0);
// getelementptr and load instruction for all elements in
// parameter .p
for (size_t origFieldNum = 0; origFieldNum < EF->getNumParameters(); origFieldNum++) {
// getelementptr
Idx[1] = llvm::ConstantInt::get(
llvm::Type::getInt32Ty(mLLVMContext), OrigFieldNumToPaddedFieldNum[origFieldNum]);
llvm::Value *Ptr = NULL;
Ptr = IB->CreateInBoundsGEP(HelperFunctionParameter, Idx);
// Load is only required for non-struct ptrs
if (isStructInput[origFieldNum]) {
Params.push_back(Ptr);
} else {
llvm::Value *V = IB->CreateLoad(Ptr);
Params.push_back(V);
}
}
// Call and pass the all elements as parameter to F
llvm::CallInst *CI = IB->CreateCall(F, Params);
CI->setCallingConv(F->getCallingConv());
if (F->getReturnType() == llvm::Type::getVoidTy(mLLVMContext)) {
IB->CreateRetVoid();
} else {
IB->CreateRet(CI);
}
delete IB;
}
}
ExportFuncInfo.push_back(
llvm::MDString::get(mLLVMContext, HelperFunctionName.c_str()));
}
mExportFuncMetadata->addOperand(
llvm::MDNode::get(mLLVMContext, ExportFuncInfo));
ExportFuncInfo.clear();
}
}
void Backend::dumpExportForEachInfo(llvm::Module *M) {
if (mExportForEachNameMetadata == nullptr) {
mExportForEachNameMetadata =
M->getOrInsertNamedMetadata(RS_EXPORT_FOREACH_NAME_MN);
}
if (mExportForEachSignatureMetadata == nullptr) {
mExportForEachSignatureMetadata =
M->getOrInsertNamedMetadata(RS_EXPORT_FOREACH_MN);
}
llvm::SmallVector<llvm::Metadata *, 1> ExportForEachName;
llvm::SmallVector<llvm::Metadata *, 1> ExportForEachInfo;
for (RSContext::const_export_foreach_iterator
I = mContext->export_foreach_begin(),
E = mContext->export_foreach_end();
I != E;
I++) {
const RSExportForEach *EFE = *I;
ExportForEachName.push_back(
llvm::MDString::get(mLLVMContext, EFE->getName().c_str()));