llvm_backend.cpp 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552
  1. #define MULTITHREAD_OBJECT_GENERATION 1
  2. #ifndef MULTITHREAD_OBJECT_GENERATION
  3. #define MULTITHREAD_OBJECT_GENERATION 0
  4. #endif
  5. #ifndef USE_SEPARATE_MODULES
  6. #define USE_SEPARATE_MODULES build_context.use_separate_modules
  7. #endif
  8. #ifndef LLVM_IGNORE_VERIFICATION
  9. #define LLVM_IGNORE_VERIFICATION 0
  10. #endif
  11. #include "llvm_backend.hpp"
  12. #include "llvm_abi.cpp"
  13. #include "llvm_backend_opt.cpp"
  14. #include "llvm_backend_general.cpp"
  15. #include "llvm_backend_debug.cpp"
  16. #include "llvm_backend_const.cpp"
  17. #include "llvm_backend_type.cpp"
  18. #include "llvm_backend_utility.cpp"
  19. #include "llvm_backend_expr.cpp"
  20. #include "llvm_backend_stmt.cpp"
  21. #include "llvm_backend_proc.cpp"
  22. gb_internal String get_default_microarchitecture() {
  23. String default_march = str_lit("generic");
  24. if (build_context.metrics.arch == TargetArch_amd64) {
  25. // NOTE(bill): x86-64-v2 is more than enough for everyone
  26. //
  27. // x86-64: CMOV, CMPXCHG8B, FPU, FXSR, MMX, FXSR, SCE, SSE, SSE2
  28. // x86-64-v2: (close to Nehalem) CMPXCHG16B, LAHF-SAHF, POPCNT, SSE3, SSE4.1, SSE4.2, SSSE3
  29. // x86-64-v3: (close to Haswell) AVX, AVX2, BMI1, BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE
  30. // x86-64-v4: AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL
  31. if (ODIN_LLVM_MINIMUM_VERSION_12) {
  32. if (build_context.metrics.os == TargetOs_freestanding) {
  33. default_march = str_lit("x86-64");
  34. } else {
  35. default_march = str_lit("x86-64-v2");
  36. }
  37. }
  38. } else if (build_context.metrics.arch == TargetArch_riscv64) {
  39. default_march = str_lit("generic-rv64");
  40. }
  41. return default_march;
  42. }
  43. gb_internal String get_final_microarchitecture() {
  44. BuildContext *bc = &build_context;
  45. String microarch = bc->microarch;
  46. if (microarch.len == 0) {
  47. microarch = get_default_microarchitecture();
  48. } else if (microarch == str_lit("native")) {
  49. microarch = make_string_c(LLVMGetHostCPUName());
  50. }
  51. return microarch;
  52. }
  53. gb_internal String get_default_features() {
  54. BuildContext *bc = &build_context;
  55. int off = 0;
  56. for (int i = 0; i < bc->metrics.arch; i += 1) {
  57. off += target_microarch_counts[i];
  58. }
  59. String microarch = get_final_microarchitecture();
  60. // NOTE(laytan): for riscv64 to work properly with Odin, we need to enforce some features.
  61. // and we also overwrite the generic target to include more features so we don't default to
  62. // a potato feature set.
  63. if (bc->metrics.arch == TargetArch_riscv64) {
  64. if (microarch == str_lit("generic-rv64")) {
  65. // This is what clang does by default (on -march=rv64gc for General Computing), seems good to also default to.
  66. String features = str_lit("64bit,a,c,d,f,m,relax,zicsr,zifencei");
  67. // Update the features string so LLVM uses it later.
  68. if (bc->target_features_string.len > 0) {
  69. bc->target_features_string = concatenate3_strings(permanent_allocator(), features, str_lit(","), bc->target_features_string);
  70. } else {
  71. bc->target_features_string = features;
  72. }
  73. return features;
  74. }
  75. }
  76. for (int i = off; i < off+target_microarch_counts[bc->metrics.arch]; i += 1) {
  77. if (microarch_features_list[i].microarch == microarch) {
  78. return microarch_features_list[i].features;
  79. }
  80. }
  81. GB_PANIC("unknown microarch: %.*s", LIT(microarch));
  82. return {};
  83. }
  84. gb_internal void lb_add_foreign_library_path(lbModule *m, Entity *e) {
  85. if (e == nullptr) {
  86. return;
  87. }
  88. GB_ASSERT(e->kind == Entity_LibraryName);
  89. GB_ASSERT(e->flags & EntityFlag_Used);
  90. mutex_lock(&m->gen->foreign_mutex);
  91. if (!ptr_set_update(&m->gen->foreign_libraries_set, e)) {
  92. array_add(&m->gen->foreign_libraries, e);
  93. }
  94. mutex_unlock(&m->gen->foreign_mutex);
  95. }
  96. gb_internal GB_COMPARE_PROC(foreign_library_cmp) {
  97. int cmp = 0;
  98. Entity *x = *(Entity **)a;
  99. Entity *y = *(Entity **)b;
  100. if (x == y) {
  101. return 0;
  102. }
  103. GB_ASSERT(x->kind == Entity_LibraryName);
  104. GB_ASSERT(y->kind == Entity_LibraryName);
  105. cmp = i64_cmp(x->LibraryName.priority_index, y->LibraryName.priority_index);
  106. if (cmp) {
  107. return cmp;
  108. }
  109. if (x->pkg != y->pkg) {
  110. isize order_x = x->pkg ? x->pkg->order : 0;
  111. isize order_y = y->pkg ? y->pkg->order : 0;
  112. cmp = isize_cmp(order_x, order_y);
  113. if (cmp) {
  114. return cmp;
  115. }
  116. }
  117. if (x->file != y->file) {
  118. String fullpath_x = x->file ? x->file->fullpath : (String{});
  119. String fullpath_y = y->file ? y->file->fullpath : (String{});
  120. String file_x = filename_from_path(fullpath_x);
  121. String file_y = filename_from_path(fullpath_y);
  122. cmp = string_compare(file_x, file_y);
  123. if (cmp) {
  124. return cmp;
  125. }
  126. }
  127. cmp = u64_cmp(x->order_in_src, y->order_in_src);
  128. if (cmp) {
  129. return cmp;
  130. }
  131. return i32_cmp(x->token.pos.offset, y->token.pos.offset);
  132. }
  133. gb_internal void lb_set_entity_from_other_modules_linkage_correctly(lbModule *other_module, Entity *e, String const &name) {
  134. if (other_module == nullptr) {
  135. return;
  136. }
  137. char const *cname = alloc_cstring(permanent_allocator(), name);
  138. mpsc_enqueue(&other_module->gen->entities_to_correct_linkage, lbEntityCorrection{other_module, e, cname});
  139. }
  140. gb_internal void lb_correct_entity_linkage(lbGenerator *gen) {
  141. for (lbEntityCorrection ec = {}; mpsc_dequeue(&gen->entities_to_correct_linkage, &ec); /**/) {
  142. LLVMValueRef other_global = nullptr;
  143. if (ec.e->kind == Entity_Variable) {
  144. other_global = LLVMGetNamedGlobal(ec.other_module->mod, ec.cname);
  145. if (other_global) {
  146. LLVMSetLinkage(other_global, LLVMWeakAnyLinkage);
  147. if (!ec.e->Variable.is_export) {
  148. LLVMSetVisibility(other_global, LLVMHiddenVisibility);
  149. }
  150. }
  151. } else if (ec.e->kind == Entity_Procedure) {
  152. other_global = LLVMGetNamedFunction(ec.other_module->mod, ec.cname);
  153. if (other_global) {
  154. LLVMSetLinkage(other_global, LLVMWeakAnyLinkage);
  155. if (!ec.e->Procedure.is_export) {
  156. LLVMSetVisibility(other_global, LLVMHiddenVisibility);
  157. }
  158. }
  159. }
  160. }
  161. }
  162. gb_internal void lb_emit_init_context(lbProcedure *p, lbAddr addr) {
  163. TEMPORARY_ALLOCATOR_GUARD();
  164. GB_ASSERT(addr.kind == lbAddr_Context);
  165. GB_ASSERT(addr.ctx.sel.index.count == 0);
  166. auto args = array_make<lbValue>(temporary_allocator(), 1);
  167. args[0] = addr.addr;
  168. lb_emit_runtime_call(p, "__init_context", args);
  169. }
  170. gb_internal lbContextData *lb_push_context_onto_stack_from_implicit_parameter(lbProcedure *p) {
  171. Type *pt = base_type(p->type);
  172. GB_ASSERT(pt->kind == Type_Proc);
  173. GB_ASSERT(pt->Proc.calling_convention == ProcCC_Odin);
  174. String name = str_lit("__.context_ptr");
  175. Entity *e = alloc_entity_param(nullptr, make_token_ident(name), t_context_ptr, false, false);
  176. e->flags |= EntityFlag_NoAlias;
  177. LLVMValueRef context_ptr = LLVMGetParam(p->value, LLVMCountParams(p->value)-1);
  178. LLVMSetValueName2(context_ptr, cast(char const *)name.text, name.len);
  179. context_ptr = LLVMBuildPointerCast(p->builder, context_ptr, lb_type(p->module, e->type), "");
  180. lbValue param = {context_ptr, e->type};
  181. lb_add_entity(p->module, e, param);
  182. lbAddr ctx_addr = {};
  183. ctx_addr.kind = lbAddr_Context;
  184. ctx_addr.addr = param;
  185. lbContextData *cd = array_add_and_get(&p->context_stack);
  186. cd->ctx = ctx_addr;
  187. cd->scope_index = -1;
  188. cd->uses = +1; // make sure it has been used already
  189. return cd;
  190. }
  191. gb_internal lbContextData *lb_push_context_onto_stack(lbProcedure *p, lbAddr ctx) {
  192. ctx.kind = lbAddr_Context;
  193. lbContextData *cd = array_add_and_get(&p->context_stack);
  194. cd->ctx = ctx;
  195. cd->scope_index = p->scope_index;
  196. return cd;
  197. }
  198. gb_internal String lb_internal_gen_name_from_type(char const *prefix, Type *type) {
  199. gbString str = gb_string_make(permanent_allocator(), prefix);
  200. gbString tcs = temp_canonical_string(type);
  201. str = gb_string_appendc(str, CANONICAL_TYPE_SEPARATOR);
  202. str = gb_string_append_length(str, tcs, gb_string_length(tcs));
  203. String proc_name = make_string(cast(u8 const *)str, gb_string_length(str));
  204. return proc_name;
  205. }
  206. gb_internal lbValue lb_equal_proc_for_type(lbModule *m, Type *type) {
  207. type = base_type(type);
  208. GB_ASSERT(is_type_comparable(type));
  209. Type *pt = alloc_type_pointer(type);
  210. LLVMTypeRef ptr_type = lb_type(m, pt);
  211. lbProcedure **found = map_get(&m->equal_procs, type);
  212. lbProcedure *compare_proc = nullptr;
  213. if (found) {
  214. compare_proc = *found;
  215. GB_ASSERT(compare_proc != nullptr);
  216. return {compare_proc->value, compare_proc->type};
  217. }
  218. String proc_name = lb_internal_gen_name_from_type("__$equal", type);
  219. lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_equal_proc);
  220. map_set(&m->equal_procs, type, p);
  221. lb_begin_procedure_body(p);
  222. // lb_add_attribute_to_proc(m, p->value, "readonly");
  223. lb_add_attribute_to_proc(m, p->value, "nounwind");
  224. LLVMValueRef x = LLVMGetParam(p->value, 0);
  225. LLVMValueRef y = LLVMGetParam(p->value, 1);
  226. x = LLVMBuildPointerCast(p->builder, x, ptr_type, "");
  227. y = LLVMBuildPointerCast(p->builder, y, ptr_type, "");
  228. lbValue lhs = {x, pt};
  229. lbValue rhs = {y, pt};
  230. lb_add_proc_attribute_at_index(p, 1+0, "nonnull");
  231. lb_add_proc_attribute_at_index(p, 1+1, "nonnull");
  232. lbBlock *block_same_ptr = lb_create_block(p, "same_ptr");
  233. lbBlock *block_diff_ptr = lb_create_block(p, "diff_ptr");
  234. lbValue same_ptr = lb_emit_comp(p, Token_CmpEq, lhs, rhs);
  235. lb_emit_if(p, same_ptr, block_same_ptr, block_diff_ptr);
  236. lb_start_block(p, block_same_ptr);
  237. LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false));
  238. lb_start_block(p, block_diff_ptr);
  239. if (type->kind == Type_Struct) {
  240. type_set_offsets(type);
  241. lbBlock *block_false = lb_create_block(p, "bfalse");
  242. lbValue res = lb_const_bool(m, t_bool, true);
  243. for_array(i, type->Struct.fields) {
  244. lbBlock *next_block = lb_create_block(p, "btrue");
  245. lbValue pleft = lb_emit_struct_ep(p, lhs, cast(i32)i);
  246. lbValue pright = lb_emit_struct_ep(p, rhs, cast(i32)i);
  247. lbValue left = lb_emit_load(p, pleft);
  248. lbValue right = lb_emit_load(p, pright);
  249. lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
  250. lb_emit_if(p, ok, next_block, block_false);
  251. lb_emit_jump(p, next_block);
  252. lb_start_block(p, next_block);
  253. }
  254. LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false));
  255. lb_start_block(p, block_false);
  256. LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 0, false));
  257. } else if (type->kind == Type_Union) {
  258. if (type_size_of(type) == 0) {
  259. LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false));
  260. } else if (is_type_union_maybe_pointer(type)) {
  261. Type *v = type->Union.variants[0];
  262. Type *pv = alloc_type_pointer(v);
  263. lbValue left = lb_emit_load(p, lb_emit_conv(p, lhs, pv));
  264. lbValue right = lb_emit_load(p, lb_emit_conv(p, rhs, pv));
  265. lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
  266. ok = lb_emit_conv(p, ok, t_bool);
  267. LLVMBuildRet(p->builder, ok.value);
  268. } else {
  269. lbBlock *block_false = lb_create_block(p, "bfalse");
  270. lbBlock *block_switch = lb_create_block(p, "bswitch");
  271. lbValue left_tag = lb_emit_load(p, lb_emit_union_tag_ptr(p, lhs));
  272. lbValue right_tag = lb_emit_load(p, lb_emit_union_tag_ptr(p, rhs));
  273. lbValue tag_eq = lb_emit_comp(p, Token_CmpEq, left_tag, right_tag);
  274. lb_emit_if(p, tag_eq, block_switch, block_false);
  275. lb_start_block(p, block_switch);
  276. unsigned variant_count = cast(unsigned)type->Union.variants.count;
  277. if (type->Union.kind != UnionType_no_nil) {
  278. variant_count += 1;
  279. }
  280. LLVMValueRef v_switch = LLVMBuildSwitch(p->builder, left_tag.value, block_false->block, variant_count);
  281. if (type->Union.kind != UnionType_no_nil) {
  282. lbBlock *case_block = lb_create_block(p, "bcase");
  283. lb_start_block(p, case_block);
  284. lbValue case_tag = lb_const_int(p->module, union_tag_type(type), 0);
  285. LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false));
  286. LLVMAddCase(v_switch, case_tag.value, case_block->block);
  287. }
  288. for (Type *v : type->Union.variants) {
  289. lbBlock *case_block = lb_create_block(p, "bcase");
  290. lb_start_block(p, case_block);
  291. lbValue case_tag = lb_const_union_tag(p->module, type, v);
  292. Type *vp = alloc_type_pointer(v);
  293. lbValue left = lb_emit_load(p, lb_emit_conv(p, lhs, vp));
  294. lbValue right = lb_emit_load(p, lb_emit_conv(p, rhs, vp));
  295. lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
  296. ok = lb_emit_conv(p, ok, t_bool);
  297. LLVMBuildRet(p->builder, ok.value);
  298. LLVMAddCase(v_switch, case_tag.value, case_block->block);
  299. }
  300. lb_start_block(p, block_false);
  301. LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 0, false));
  302. }
  303. } else {
  304. lbValue left = lb_emit_load(p, lhs);
  305. lbValue right = lb_emit_load(p, rhs);
  306. lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
  307. ok = lb_emit_conv(p, ok, t_bool);
  308. LLVMBuildRet(p->builder, ok.value);
  309. }
  310. lb_end_procedure_body(p);
  311. compare_proc = p;
  312. return {compare_proc->value, compare_proc->type};
  313. }
  314. gb_internal lbValue lb_simple_compare_hash(lbProcedure *p, Type *type, lbValue data, lbValue seed) {
  315. TEMPORARY_ALLOCATOR_GUARD();
  316. GB_ASSERT_MSG(is_type_simple_compare(type), "%s", type_to_string(type));
  317. auto args = array_make<lbValue>(temporary_allocator(), 3);
  318. args[0] = data;
  319. args[1] = seed;
  320. args[2] = lb_const_int(p->module, t_int, type_size_of(type));
  321. return lb_emit_runtime_call(p, "default_hasher", args);
  322. }
  323. gb_internal void lb_add_callsite_force_inline(lbProcedure *p, lbValue ret_value) {
  324. LLVMAddCallSiteAttribute(ret_value.value, LLVMAttributeIndex_FunctionIndex, lb_create_enum_attribute(p->module->ctx, "alwaysinline"));
  325. }
  326. gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) {
  327. type = core_type(type);
  328. GB_ASSERT_MSG(is_type_comparable(type), "%s", type_to_string(type));
  329. Type *pt = alloc_type_pointer(type);
  330. lbProcedure **found = map_get(&m->hasher_procs, type);
  331. if (found) {
  332. GB_ASSERT(*found != nullptr);
  333. return {(*found)->value, (*found)->type};
  334. }
  335. String proc_name = lb_internal_gen_name_from_type("__$hasher", type);
  336. lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_hasher_proc);
  337. map_set(&m->hasher_procs, type, p);
  338. lb_begin_procedure_body(p);
  339. defer (lb_end_procedure_body(p));
  340. // lb_add_attribute_to_proc(m, p->value, "readonly");
  341. lb_add_attribute_to_proc(m, p->value, "nounwind");
  342. LLVMValueRef x = LLVMGetParam(p->value, 0);
  343. LLVMValueRef y = LLVMGetParam(p->value, 1);
  344. lbValue data = {x, t_rawptr};
  345. lbValue seed = {y, t_uintptr};
  346. lb_add_proc_attribute_at_index(p, 1+0, "nonnull");
  347. // lb_add_proc_attribute_at_index(p, 1+0, "readonly");
  348. if (is_type_simple_compare(type)) {
  349. lbValue res = lb_simple_compare_hash(p, type, data, seed);
  350. lb_add_callsite_force_inline(p, res);
  351. LLVMBuildRet(p->builder, res.value);
  352. return {p->value, p->type};
  353. }
  354. TEMPORARY_ALLOCATOR_GUARD();
  355. if (type->kind == Type_Struct) {
  356. type_set_offsets(type);
  357. data = lb_emit_conv(p, data, t_u8_ptr);
  358. auto args = array_make<lbValue>(temporary_allocator(), 2);
  359. for_array(i, type->Struct.fields) {
  360. GB_ASSERT(type->Struct.offsets != nullptr);
  361. i64 offset = type->Struct.offsets[i];
  362. Entity *field = type->Struct.fields[i];
  363. lbValue field_hasher = lb_hasher_proc_for_type(m, field->type);
  364. lbValue ptr = lb_emit_ptr_offset(p, data, lb_const_int(m, t_uintptr, offset));
  365. args[0] = ptr;
  366. args[1] = seed;
  367. seed = lb_emit_call(p, field_hasher, args);
  368. }
  369. LLVMBuildRet(p->builder, seed.value);
  370. } else if (type->kind == Type_Union) {
  371. auto args = array_make<lbValue>(temporary_allocator(), 2);
  372. if (is_type_union_maybe_pointer(type)) {
  373. Type *v = type->Union.variants[0];
  374. lbValue variant_hasher = lb_hasher_proc_for_type(m, v);
  375. args[0] = data;
  376. args[1] = seed;
  377. lbValue res = lb_emit_call(p, variant_hasher, args);
  378. lb_add_callsite_force_inline(p, res);
  379. LLVMBuildRet(p->builder, res.value);
  380. }
  381. lbBlock *end_block = lb_create_block(p, "bend");
  382. data = lb_emit_conv(p, data, pt);
  383. lbValue tag_ptr = lb_emit_union_tag_ptr(p, data);
  384. lbValue tag = lb_emit_load(p, tag_ptr);
  385. LLVMValueRef v_switch = LLVMBuildSwitch(p->builder, tag.value, end_block->block, cast(unsigned)type->Union.variants.count);
  386. for (Type *v : type->Union.variants) {
  387. lbBlock *case_block = lb_create_block(p, "bcase");
  388. lb_start_block(p, case_block);
  389. lbValue case_tag = lb_const_union_tag(p->module, type, v);
  390. lbValue variant_hasher = lb_hasher_proc_for_type(m, v);
  391. args[0] = data;
  392. args[1] = seed;
  393. lbValue res = lb_emit_call(p, variant_hasher, args);
  394. LLVMBuildRet(p->builder, res.value);
  395. LLVMAddCase(v_switch, case_tag.value, case_block->block);
  396. }
  397. lb_start_block(p, end_block);
  398. LLVMBuildRet(p->builder, seed.value);
  399. } else if (type->kind == Type_Array) {
  400. lbAddr pres = lb_add_local_generated(p, t_uintptr, false);
  401. lb_addr_store(p, pres, seed);
  402. auto args = array_make<lbValue>(temporary_allocator(), 2);
  403. lbValue elem_hasher = lb_hasher_proc_for_type(m, type->Array.elem);
  404. auto loop_data = lb_loop_start(p, cast(isize)type->Array.count, t_i32);
  405. data = lb_emit_conv(p, data, pt);
  406. lbValue ptr = lb_emit_array_ep(p, data, loop_data.idx);
  407. args[0] = ptr;
  408. args[1] = lb_addr_load(p, pres);
  409. lbValue new_seed = lb_emit_call(p, elem_hasher, args);
  410. lb_addr_store(p, pres, new_seed);
  411. lb_loop_end(p, loop_data);
  412. lbValue res = lb_addr_load(p, pres);
  413. LLVMBuildRet(p->builder, res.value);
  414. } else if (type->kind == Type_EnumeratedArray) {
  415. lbAddr res = lb_add_local_generated(p, t_uintptr, false);
  416. lb_addr_store(p, res, seed);
  417. auto args = array_make<lbValue>(temporary_allocator(), 2);
  418. lbValue elem_hasher = lb_hasher_proc_for_type(m, type->EnumeratedArray.elem);
  419. auto loop_data = lb_loop_start(p, cast(isize)type->EnumeratedArray.count, t_i32);
  420. data = lb_emit_conv(p, data, pt);
  421. lbValue ptr = lb_emit_array_ep(p, data, loop_data.idx);
  422. args[0] = ptr;
  423. args[1] = lb_addr_load(p, res);
  424. lbValue new_seed = lb_emit_call(p, elem_hasher, args);
  425. lb_addr_store(p, res, new_seed);
  426. lb_loop_end(p, loop_data);
  427. lbValue vres = lb_addr_load(p, res);
  428. LLVMBuildRet(p->builder, vres.value);
  429. } else if (is_type_cstring(type)) {
  430. auto args = array_make<lbValue>(temporary_allocator(), 2);
  431. args[0] = data;
  432. args[1] = seed;
  433. lbValue res = lb_emit_runtime_call(p, "default_hasher_cstring", args);
  434. lb_add_callsite_force_inline(p, res);
  435. LLVMBuildRet(p->builder, res.value);
  436. } else if (is_type_string(type)) {
  437. auto args = array_make<lbValue>(temporary_allocator(), 2);
  438. args[0] = data;
  439. args[1] = seed;
  440. lbValue res = lb_emit_runtime_call(p, "default_hasher_string", args);
  441. lb_add_callsite_force_inline(p, res);
  442. LLVMBuildRet(p->builder, res.value);
  443. } else {
  444. GB_PANIC("Unhandled type for hasher: %s", type_to_string(type));
  445. }
  446. return {p->value, p->type};
  447. }
  448. #define LLVM_SET_VALUE_NAME(value, name) LLVMSetValueName2((value), (name), gb_count_of((name))-1);
  449. gb_internal lbValue lb_map_get_proc_for_type(lbModule *m, Type *type) {
  450. GB_ASSERT(!build_context.dynamic_map_calls);
  451. type = base_type(type);
  452. GB_ASSERT(type->kind == Type_Map);
  453. lbProcedure **found = map_get(&m->map_get_procs, type);
  454. if (found) {
  455. GB_ASSERT(*found != nullptr);
  456. return {(*found)->value, (*found)->type};
  457. }
  458. String proc_name = lb_internal_gen_name_from_type("__$map_get", type);
  459. lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_map_get_proc);
  460. map_set(&m->map_get_procs, type, p);
  461. lb_begin_procedure_body(p);
  462. defer (lb_end_procedure_body(p));
  463. LLVMSetLinkage(p->value, LLVMInternalLinkage);
  464. lb_add_attribute_to_proc(m, p->value, "nounwind");
  465. if (build_context.ODIN_DEBUG) {
  466. lb_add_attribute_to_proc(m, p->value, "noinline");
  467. }
  468. LLVMValueRef x = LLVMGetParam(p->value, 0);
  469. LLVMValueRef y = LLVMGetParam(p->value, 1);
  470. LLVMValueRef z = LLVMGetParam(p->value, 2);
  471. lbValue map_ptr = {x, t_rawptr};
  472. lbValue h = {y, t_uintptr};
  473. lbValue key_ptr = {z, t_rawptr};
  474. LLVM_SET_VALUE_NAME(h.value, "hash");
  475. lb_add_proc_attribute_at_index(p, 1+0, "nonnull");
  476. lb_add_proc_attribute_at_index(p, 1+0, "readonly");
  477. lb_add_proc_attribute_at_index(p, 1+2, "nonnull");
  478. lb_add_proc_attribute_at_index(p, 1+2, "readonly");
  479. lbBlock *loop_block = lb_create_block(p, "loop");
  480. lbBlock *hash_block = lb_create_block(p, "hash");
  481. lbBlock *probe_block = lb_create_block(p, "probe");
  482. lbBlock *increment_block = lb_create_block(p, "increment");
  483. lbBlock *hash_compare_block = lb_create_block(p, "hash_compare");
  484. lbBlock *key_compare_block = lb_create_block(p, "key_compare");
  485. lbBlock *value_block = lb_create_block(p, "value");
  486. lbBlock *nil_block = lb_create_block(p, "nil");
  487. map_ptr = lb_emit_conv(p, map_ptr, t_raw_map_ptr);
  488. LLVM_SET_VALUE_NAME(map_ptr.value, "map_ptr");
  489. lbValue map = lb_emit_load(p, map_ptr);
  490. LLVM_SET_VALUE_NAME(map.value, "map");
  491. lbValue length = lb_map_len(p, map);
  492. LLVM_SET_VALUE_NAME(length.value, "length");
  493. lb_emit_if(p, lb_emit_comp(p, Token_CmpEq, length, lb_const_nil(m, t_int)), nil_block, hash_block);
  494. lb_start_block(p, hash_block);
  495. key_ptr = lb_emit_conv(p, key_ptr, alloc_type_pointer(type->Map.key));
  496. LLVM_SET_VALUE_NAME(key_ptr.value, "key_ptr");
  497. lbValue key = lb_emit_load(p, key_ptr);
  498. LLVM_SET_VALUE_NAME(key.value, "key");
  499. lbAddr pos = lb_add_local_generated(p, t_uintptr, false);
  500. lbAddr distance = lb_add_local_generated(p, t_uintptr, true);
  501. LLVM_SET_VALUE_NAME(pos.addr.value, "pos");
  502. LLVM_SET_VALUE_NAME(distance.addr.value, "distance");
  503. lbValue capacity = lb_map_cap(p, map);
  504. LLVM_SET_VALUE_NAME(capacity.value, "capacity");
  505. lbValue cap_minus_1 = lb_emit_arith(p, Token_Sub, capacity, lb_const_int(m, t_int, 1), t_int);
  506. lbValue mask = lb_emit_conv(p, cap_minus_1, t_uintptr);
  507. LLVM_SET_VALUE_NAME(mask.value, "mask");
  508. {
  509. // map_desired_position inlined
  510. lbValue the_pos = lb_emit_arith(p, Token_And, h, mask, t_uintptr);
  511. the_pos = lb_emit_conv(p, the_pos, t_uintptr);
  512. lb_addr_store(p, pos, the_pos);
  513. }
  514. lbValue zero_uintptr = lb_const_int(m, t_uintptr, 0);
  515. lbValue one_uintptr = lb_const_int(m, t_uintptr, 1);
  516. lbValue ks = lb_map_data_uintptr(p, map);
  517. lbValue vs = lb_map_cell_index_static(p, type->Map.key, ks, capacity);
  518. lbValue hs = lb_map_cell_index_static(p, type->Map.value, vs, capacity);
  519. ks = lb_emit_conv(p, ks, alloc_type_pointer(type->Map.key));
  520. vs = lb_emit_conv(p, vs, alloc_type_pointer(type->Map.value));
  521. hs = lb_emit_conv(p, hs, alloc_type_pointer(t_uintptr));
  522. LLVM_SET_VALUE_NAME(ks.value, "ks");
  523. LLVM_SET_VALUE_NAME(vs.value, "vs");
  524. LLVM_SET_VALUE_NAME(hs.value, "hs");
  525. lb_emit_jump(p, loop_block);
  526. lb_start_block(p, loop_block);
  527. lbValue element_hash = lb_emit_load(p, lb_emit_ptr_offset(p, hs, lb_addr_load(p, pos)));
  528. LLVM_SET_VALUE_NAME(element_hash.value, "element_hash");
  529. {
  530. // if element_hash == 0 { return nil }
  531. lb_emit_if(p, lb_emit_comp(p, Token_CmpEq, element_hash, zero_uintptr), nil_block, probe_block);
  532. }
  533. lb_start_block(p, probe_block);
  534. {
  535. // map_probe_distance inlined
  536. lbValue probe_distance = lb_emit_arith(p, Token_And, h, mask, t_uintptr);
  537. probe_distance = lb_emit_conv(p, probe_distance, t_uintptr);
  538. lbValue cap = lb_emit_conv(p, capacity, t_uintptr);
  539. lbValue base = lb_emit_arith(p, Token_Add, lb_addr_load(p, pos), cap, t_uintptr);
  540. probe_distance = lb_emit_arith(p, Token_Sub, base, probe_distance, t_uintptr);
  541. probe_distance = lb_emit_arith(p, Token_And, probe_distance, mask, t_uintptr);
  542. LLVM_SET_VALUE_NAME(probe_distance.value, "probe_distance");
  543. lbValue cond = lb_emit_comp(p, Token_Gt, lb_addr_load(p, distance), probe_distance);
  544. lb_emit_if(p, cond, nil_block, hash_compare_block);
  545. }
  546. lb_start_block(p, hash_compare_block);
  547. {
  548. lb_emit_if(p, lb_emit_comp(p, Token_CmpEq, element_hash, h), key_compare_block, increment_block);
  549. }
  550. lb_start_block(p, key_compare_block);
  551. {
  552. lbValue element_key = lb_map_cell_index_static(p, type->Map.key, ks, lb_addr_load(p, pos));
  553. element_key = lb_emit_conv(p, element_key, ks.type);
  554. LLVM_SET_VALUE_NAME(element_key.value, "element_key_ptr");
  555. lbValue cond = lb_emit_comp(p, Token_CmpEq, lb_emit_load(p, element_key), key);
  556. lb_emit_if(p, cond, value_block, increment_block);
  557. }
  558. lb_start_block(p, value_block);
  559. {
  560. lbValue element_value = lb_map_cell_index_static(p, type->Map.value, vs, lb_addr_load(p, pos));
  561. LLVM_SET_VALUE_NAME(element_value.value, "element_value_ptr");
  562. element_value = lb_emit_conv(p, element_value, t_rawptr);
  563. LLVMBuildRet(p->builder, element_value.value);
  564. }
  565. lb_start_block(p, increment_block);
  566. {
  567. lbValue pp = lb_addr_load(p, pos);
  568. pp = lb_emit_arith(p, Token_Add, pp, one_uintptr, t_uintptr);
  569. pp = lb_emit_arith(p, Token_And, pp, mask, t_uintptr);
  570. lb_addr_store(p, pos, pp);
  571. lb_emit_increment(p, distance.addr);
  572. }
  573. lb_emit_jump(p, loop_block);
  574. lb_start_block(p, nil_block);
  575. {
  576. lbValue res = lb_const_nil(m, t_rawptr);
  577. LLVMBuildRet(p->builder, res.value);
  578. }
  579. // gb_printf_err("%s\n", LLVMPrintValueToString(p->value));
  580. return {p->value, p->type};
  581. }
  582. // gb_internal void lb_debug_print(lbProcedure *p, String const &str) {
  583. // auto args = array_make<lbValue>(heap_allocator(), 1);
  584. // args[0] = lb_const_string(p->module, str);
  585. // lb_emit_runtime_call(p, "print_string", args);
  586. // }
  587. gb_internal lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) {
  588. TEMPORARY_ALLOCATOR_GUARD();
  589. GB_ASSERT(!build_context.dynamic_map_calls);
  590. type = base_type(type);
  591. GB_ASSERT(type->kind == Type_Map);
  592. lbProcedure **found = map_get(&m->map_set_procs, type);
  593. if (found) {
  594. GB_ASSERT(*found != nullptr);
  595. return {(*found)->value, (*found)->type};
  596. }
  597. String proc_name = lb_internal_gen_name_from_type("__$map_set", type);
  598. lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_map_set_proc);
  599. map_set(&m->map_set_procs, type, p);
  600. lb_begin_procedure_body(p);
  601. defer (lb_end_procedure_body(p));
  602. LLVMSetLinkage(p->value, LLVMInternalLinkage);
  603. lb_add_attribute_to_proc(m, p->value, "nounwind");
  604. if (build_context.ODIN_DEBUG) {
  605. lb_add_attribute_to_proc(m, p->value, "noinline");
  606. }
  607. lbValue map_ptr = {LLVMGetParam(p->value, 0), t_rawptr};
  608. lbValue hash_param = {LLVMGetParam(p->value, 1), t_uintptr};
  609. lbValue key_ptr = {LLVMGetParam(p->value, 2), t_rawptr};
  610. lbValue value_ptr = {LLVMGetParam(p->value, 3), t_rawptr};
  611. lbValue location_ptr = {LLVMGetParam(p->value, 4), t_source_code_location_ptr};
  612. map_ptr = lb_emit_conv(p, map_ptr, alloc_type_pointer(type));
  613. key_ptr = lb_emit_conv(p, key_ptr, alloc_type_pointer(type->Map.key));
  614. LLVM_SET_VALUE_NAME(map_ptr.value, "map_ptr");
  615. LLVM_SET_VALUE_NAME(hash_param.value, "hash_param");
  616. LLVM_SET_VALUE_NAME(key_ptr.value, "key_ptr");
  617. LLVM_SET_VALUE_NAME(value_ptr.value, "value_ptr");
  618. LLVM_SET_VALUE_NAME(location_ptr.value, "location");
  619. lb_add_proc_attribute_at_index(p, 1+0, "nonnull");
  620. lb_add_proc_attribute_at_index(p, 1+0, "noalias");
  621. lb_add_proc_attribute_at_index(p, 1+2, "nonnull");
  622. if (!are_types_identical(type->Map.key, type->Map.value)) {
  623. lb_add_proc_attribute_at_index(p, 1+2, "noalias");
  624. }
  625. lb_add_proc_attribute_at_index(p, 1+2, "readonly");
  626. lb_add_proc_attribute_at_index(p, 1+3, "nonnull");
  627. if (!are_types_identical(type->Map.key, type->Map.value)) {
  628. lb_add_proc_attribute_at_index(p, 1+3, "noalias");
  629. }
  630. lb_add_proc_attribute_at_index(p, 1+3, "readonly");
  631. lb_add_proc_attribute_at_index(p, 1+4, "nonnull");
  632. lb_add_proc_attribute_at_index(p, 1+4, "noalias");
  633. lb_add_proc_attribute_at_index(p, 1+4, "readonly");
  634. lbAddr hash_addr = lb_add_local_generated(p, t_uintptr, false);
  635. lb_addr_store(p, hash_addr, hash_param);
  636. LLVM_SET_VALUE_NAME(hash_addr.addr.value, "hash");
  637. ////
  638. lbValue found_ptr = {};
  639. {
  640. lbValue map_get_proc = lb_map_get_proc_for_type(m, type);
  641. auto args = array_make<lbValue>(temporary_allocator(), 3);
  642. args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
  643. args[1] = lb_addr_load(p, hash_addr);
  644. args[2] = key_ptr;
  645. found_ptr = lb_emit_call(p, map_get_proc, args);
  646. }
  647. LLVM_SET_VALUE_NAME(found_ptr.value, "found_ptr");
  648. lbBlock *found_block = lb_create_block(p, "found");
  649. lbBlock *check_grow_block = lb_create_block(p, "check-grow");
  650. lbBlock *grow_fail_block = lb_create_block(p, "grow-fail");
  651. lbBlock *insert_block = lb_create_block(p, "insert");
  652. lbBlock *check_has_grown_block = lb_create_block(p, "check-has-grown");
  653. lbBlock *rehash_block = lb_create_block(p, "rehash");
  654. lb_emit_if(p, lb_emit_comp_against_nil(p, Token_NotEq, found_ptr), found_block, check_grow_block);
  655. lb_start_block(p, found_block);
  656. {
  657. lb_mem_copy_non_overlapping(p, found_ptr, value_ptr, lb_const_int(m, t_int, type_size_of(type->Map.value)));
  658. LLVMBuildRet(p->builder, lb_emit_conv(p, found_ptr, t_rawptr).value);
  659. }
  660. lb_start_block(p, check_grow_block);
  661. lbValue map_info = lb_gen_map_info_ptr(p->module, type);
  662. LLVM_SET_VALUE_NAME(map_info.value, "map_info");
  663. {
  664. auto args = array_make<lbValue>(temporary_allocator(), 3);
  665. args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
  666. args[1] = map_info;
  667. args[2] = lb_emit_load(p, location_ptr);
  668. lbValue grow_err_and_has_grown = lb_emit_runtime_call(p, "__dynamic_map_check_grow", args);
  669. lbValue grow_err = lb_emit_struct_ev(p, grow_err_and_has_grown, 0);
  670. lbValue has_grown = lb_emit_struct_ev(p, grow_err_and_has_grown, 1);
  671. LLVM_SET_VALUE_NAME(grow_err.value, "grow_err");
  672. LLVM_SET_VALUE_NAME(has_grown.value, "has_grown");
  673. lb_emit_if(p, lb_emit_comp_against_nil(p, Token_NotEq, grow_err), grow_fail_block, check_has_grown_block);
  674. lb_start_block(p, grow_fail_block);
  675. LLVMBuildRet(p->builder, LLVMConstNull(lb_type(m, t_rawptr)));
  676. lb_start_block(p, check_has_grown_block);
  677. lb_emit_if(p, has_grown, rehash_block, insert_block);
  678. lb_start_block(p, rehash_block);
  679. lbValue key = lb_emit_load(p, key_ptr);
  680. lbValue new_hash = lb_gen_map_key_hash(p, map_ptr, key, nullptr);
  681. LLVM_SET_VALUE_NAME(new_hash.value, "new_hash");
  682. lb_addr_store(p, hash_addr, new_hash);
  683. lb_emit_jump(p, insert_block);
  684. }
  685. lb_start_block(p, insert_block);
  686. {
  687. auto args = array_make<lbValue>(temporary_allocator(), 5);
  688. args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
  689. args[1] = map_info;
  690. args[2] = lb_addr_load(p, hash_addr);
  691. args[3] = lb_emit_conv(p, key_ptr, t_uintptr);
  692. args[4] = lb_emit_conv(p, value_ptr, t_uintptr);
  693. lbValue result = lb_emit_runtime_call(p, "map_insert_hash_dynamic", args);
  694. lb_emit_increment(p, lb_map_len_ptr(p, map_ptr));
  695. LLVMBuildRet(p->builder, lb_emit_conv(p, result, t_rawptr).value);
  696. }
  697. return {p->value, p->type};
  698. }
  699. gb_internal lbValue lb_gen_map_cell_info_ptr(lbModule *m, Type *type) {
  700. lbAddr *found = map_get(&m->map_cell_info_map, type);
  701. if (found) {
  702. return found->addr;
  703. }
  704. i64 size = 0, len = 0;
  705. map_cell_size_and_len(type, &size, &len);
  706. LLVMValueRef const_values[4] = {};
  707. const_values[0] = lb_const_int(m, t_uintptr, type_size_of(type)).value;
  708. const_values[1] = lb_const_int(m, t_uintptr, type_align_of(type)).value;
  709. const_values[2] = lb_const_int(m, t_uintptr, size).value;
  710. const_values[3] = lb_const_int(m, t_uintptr, len).value;
  711. LLVMValueRef llvm_res = llvm_const_named_struct(m, t_map_cell_info, const_values, gb_count_of(const_values));
  712. lbValue res = {llvm_res, t_map_cell_info};
  713. lbAddr addr = lb_add_global_generated_with_name(m, t_map_cell_info, res, lb_internal_gen_name_from_type("ggv$map_cell_info", type));
  714. lb_make_global_private_const(addr);
  715. map_set(&m->map_cell_info_map, type, addr);
  716. return addr.addr;
  717. }
  718. gb_internal lbValue lb_gen_map_info_ptr(lbModule *m, Type *map_type) {
  719. map_type = base_type(map_type);
  720. GB_ASSERT(map_type->kind == Type_Map);
  721. lbAddr *found = map_get(&m->map_info_map, map_type);
  722. if (found) {
  723. return found->addr;
  724. }
  725. GB_ASSERT(t_map_info != nullptr);
  726. GB_ASSERT(t_map_cell_info != nullptr);
  727. LLVMValueRef key_cell_info = lb_gen_map_cell_info_ptr(m, map_type->Map.key).value;
  728. LLVMValueRef value_cell_info = lb_gen_map_cell_info_ptr(m, map_type->Map.value).value;
  729. LLVMValueRef const_values[4] = {};
  730. const_values[0] = key_cell_info;
  731. const_values[1] = value_cell_info;
  732. const_values[2] = lb_hasher_proc_for_type(m, map_type->Map.key).value;
  733. const_values[3] = lb_equal_proc_for_type(m, map_type->Map.key).value;
  734. LLVMValueRef llvm_res = llvm_const_named_struct(m, t_map_info, const_values, gb_count_of(const_values));
  735. lbValue res = {llvm_res, t_map_info};
  736. lbAddr addr = lb_add_global_generated_with_name(m, t_map_info, res, lb_internal_gen_name_from_type("ggv$map_info", map_type));
  737. lb_make_global_private_const(addr);
  738. map_set(&m->map_info_map, map_type, addr);
  739. return addr.addr;
  740. }
  741. gb_internal lbValue lb_const_hash(lbModule *m, lbValue key, Type *key_type) {
  742. if (true) {
  743. return {};
  744. }
  745. lbValue hashed_key = {};
  746. #if 0
  747. if (lb_is_const(key)) {
  748. u64 hash = 0xcbf29ce484222325;
  749. if (is_type_cstring(key_type)) {
  750. size_t length = 0;
  751. char const *text = LLVMGetAsString(key.value, &length);
  752. hash = fnv64a(text, cast(isize)length);
  753. } else if (is_type_string(key_type)) {
  754. unsigned data_indices[] = {0};
  755. unsigned len_indices[] = {1};
  756. LLVMValueRef data = LLVMConstExtractValue(key.value, data_indices, gb_count_of(data_indices));
  757. LLVMValueRef len = LLVMConstExtractValue(key.value, len_indices, gb_count_of(len_indices));
  758. i64 length = LLVMConstIntGetSExtValue(len);
  759. char const *text = nullptr;
  760. if (false && length != 0) {
  761. if (LLVMGetConstOpcode(data) != LLVMGetElementPtr) {
  762. return {};
  763. }
  764. // TODO(bill): THIS IS BROKEN! THIS NEEDS FIXING :P
  765. size_t ulength = 0;
  766. text = LLVMGetAsString(data, &ulength);
  767. gb_printf_err("%lld %llu %s\n", length, ulength, text);
  768. length = gb_min(length, cast(i64)ulength);
  769. }
  770. hash = fnv64a(text, cast(isize)length);
  771. } else {
  772. return {};
  773. }
  774. // TODO(bill): other const hash types
  775. if (build_context.word_size == 4) {
  776. hash &= 0xffffffffull;
  777. }
  778. hashed_key = lb_const_int(m, t_uintptr, hash);
  779. }
  780. #endif
  781. return hashed_key;
  782. }
  783. gb_internal lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue const &map_ptr, lbValue key, lbValue *key_ptr_) {
  784. TEMPORARY_ALLOCATOR_GUARD();
  785. Type* key_type = base_type(type_deref(map_ptr.type))->Map.key;
  786. lbValue real_key = lb_emit_conv(p, key, key_type);
  787. lbValue key_ptr = lb_address_from_load_or_generate_local(p, real_key);
  788. key_ptr = lb_emit_conv(p, key_ptr, t_rawptr);
  789. if (key_ptr_) *key_ptr_ = key_ptr;
  790. lbValue hashed_key = lb_const_hash(p->module, real_key, key_type);
  791. if (hashed_key.value == nullptr) {
  792. lbValue hasher = lb_hasher_proc_for_type(p->module, key_type);
  793. lbValue seed = {};
  794. {
  795. auto args = array_make<lbValue>(temporary_allocator(), 1);
  796. args[0] = lb_map_data_uintptr(p, lb_emit_load(p, map_ptr));
  797. seed = lb_emit_runtime_call(p, "map_seed_from_map_data", args);
  798. }
  799. auto args = array_make<lbValue>(temporary_allocator(), 2);
  800. args[0] = key_ptr;
  801. args[1] = seed;
  802. hashed_key = lb_emit_call(p, hasher, args);
  803. }
  804. return hashed_key;
  805. }
  806. gb_internal lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key) {
  807. TEMPORARY_ALLOCATOR_GUARD();
  808. Type *map_type = base_type(type_deref(map_ptr.type));
  809. GB_ASSERT(map_type->kind == Type_Map);
  810. lbValue ptr = {};
  811. lbValue key_ptr = {};
  812. lbValue hash = lb_gen_map_key_hash(p, map_ptr, key, &key_ptr);
  813. if (build_context.dynamic_map_calls) {
  814. auto args = array_make<lbValue>(temporary_allocator(), 4);
  815. args[0] = lb_emit_transmute(p, map_ptr, t_raw_map_ptr);
  816. args[1] = lb_gen_map_info_ptr(p->module, map_type);
  817. args[2] = hash;
  818. args[3] = key_ptr;
  819. ptr = lb_emit_runtime_call(p, "__dynamic_map_get", args);
  820. } else {
  821. lbValue map_get_proc = lb_map_get_proc_for_type(p->module, map_type);
  822. auto args = array_make<lbValue>(temporary_allocator(), 3);
  823. args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
  824. args[1] = hash;
  825. args[2] = key_ptr;
  826. ptr = lb_emit_call(p, map_get_proc, args);
  827. }
  828. return lb_emit_conv(p, ptr, alloc_type_pointer(map_type->Map.value));
  829. }
  830. gb_internal void lb_internal_dynamic_map_set(lbProcedure *p, lbValue const &map_ptr, Type *map_type,
  831. lbValue const &map_key, lbValue const &map_value, Ast *node) {
  832. TEMPORARY_ALLOCATOR_GUARD();
  833. map_type = base_type(map_type);
  834. GB_ASSERT(map_type->kind == Type_Map);
  835. lbValue key_ptr = {};
  836. lbValue hash = lb_gen_map_key_hash(p, map_ptr, map_key, &key_ptr);
  837. lbValue v = lb_emit_conv(p, map_value, map_type->Map.value);
  838. lbValue value_ptr = lb_address_from_load_or_generate_local(p, v);
  839. if (build_context.dynamic_map_calls) {
  840. auto args = array_make<lbValue>(temporary_allocator(), 6);
  841. args[0] = lb_emit_conv(p, map_ptr, t_raw_map_ptr);
  842. args[1] = lb_gen_map_info_ptr(p->module, map_type);
  843. args[2] = hash;
  844. args[3] = lb_emit_conv(p, key_ptr, t_rawptr);
  845. args[4] = lb_emit_conv(p, value_ptr, t_rawptr);
  846. args[5] = lb_emit_source_code_location_as_global(p, node);
  847. lb_emit_runtime_call(p, "__dynamic_map_set", args);
  848. } else {
  849. lbValue map_set_proc = lb_map_set_proc_for_type(p->module, map_type);
  850. auto args = array_make<lbValue>(temporary_allocator(), 5);
  851. args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
  852. args[1] = hash;
  853. args[2] = lb_emit_conv(p, key_ptr, t_rawptr);
  854. args[3] = lb_emit_conv(p, value_ptr, t_rawptr);
  855. args[4] = lb_emit_source_code_location_as_global(p, node);
  856. lb_emit_call(p, map_set_proc, args);
  857. }
  858. }
  859. gb_internal lbValue lb_dynamic_map_reserve(lbProcedure *p, lbValue const &map_ptr, isize const capacity, TokenPos const &pos) {
  860. TEMPORARY_ALLOCATOR_GUARD();
  861. String proc_name = {};
  862. if (p->entity) {
  863. proc_name = p->entity->token.string;
  864. }
  865. auto args = array_make<lbValue>(temporary_allocator(), 4);
  866. args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
  867. args[1] = lb_gen_map_info_ptr(p->module, type_deref(map_ptr.type));
  868. args[2] = lb_const_int(p->module, t_uint, capacity);
  869. args[3] = lb_emit_source_code_location_as_global(p, proc_name, pos);
  870. return lb_emit_runtime_call(p, "__dynamic_map_reserve", args);
  871. }
  872. struct lbGlobalVariable {
  873. lbValue var;
  874. lbValue init;
  875. DeclInfo *decl;
  876. bool is_initialized;
  877. };
  878. gb_internal lbProcedure *lb_create_objc_names(lbModule *main_module) {
  879. if (build_context.metrics.os != TargetOs_darwin) {
  880. return nullptr;
  881. }
  882. Type *proc_type = alloc_type_proc(nullptr, nullptr, 0, nullptr, 0, false, ProcCC_CDecl);
  883. lbProcedure *p = lb_create_dummy_procedure(main_module, str_lit("__$init_objc_names"), proc_type);
  884. lb_add_attribute_to_proc(p->module, p->value, "nounwind");
  885. p->is_startup = true;
  886. return p;
  887. }
  888. gb_internal void lb_finalize_objc_names(lbProcedure *p) {
  889. if (p == nullptr) {
  890. return;
  891. }
  892. lbModule *m = p->module;
  893. TEMPORARY_ALLOCATOR_GUARD();
  894. auto args = array_make<lbValue>(temporary_allocator(), 1);
  895. LLVMSetLinkage(p->value, LLVMInternalLinkage);
  896. lb_begin_procedure_body(p);
  897. for (auto const &entry : m->objc_classes) {
  898. String name = entry.key;
  899. args[0] = lb_const_value(m, t_cstring, exact_value_string(name));
  900. lbValue ptr = lb_emit_runtime_call(p, "objc_lookUpClass", args);
  901. lb_addr_store(p, entry.value.local_module_addr, ptr);
  902. }
  903. for (auto const &entry : m->objc_selectors) {
  904. String name = entry.key;
  905. args[0] = lb_const_value(m, t_cstring, exact_value_string(name));
  906. lbValue ptr = lb_emit_runtime_call(p, "sel_registerName", args);
  907. lb_addr_store(p, entry.value.local_module_addr, ptr);
  908. }
  909. lb_end_procedure_body(p);
  910. }
  911. gb_internal void lb_verify_function(lbModule *m, lbProcedure *p, bool dump_ll=false) {
  912. if (LLVM_IGNORE_VERIFICATION) {
  913. return;
  914. }
  915. if (!m->debug_builder && LLVMVerifyFunction(p->value, LLVMReturnStatusAction)) {
  916. char *llvm_error = nullptr;
  917. gb_printf_err("LLVM CODE GEN FAILED FOR PROCEDURE: %.*s\n", LIT(p->name));
  918. LLVMDumpValue(p->value);
  919. gb_printf_err("\n");
  920. if (dump_ll) {
  921. gb_printf_err("\n\n\n");
  922. String filepath_ll = lb_filepath_ll_for_module(m);
  923. if (LLVMPrintModuleToFile(m->mod, cast(char const *)filepath_ll.text, &llvm_error)) {
  924. gb_printf_err("LLVM Error: %s\n", llvm_error);
  925. }
  926. }
  927. LLVMVerifyFunction(p->value, LLVMPrintMessageAction);
  928. exit_with_errors();
  929. }
  930. }
  931. gb_internal WORKER_TASK_PROC(lb_llvm_module_verification_worker_proc) {
  932. char *llvm_error = nullptr;
  933. defer (LLVMDisposeMessage(llvm_error));
  934. lbModule *m = cast(lbModule *)data;
  935. if (LLVMVerifyModule(m->mod, LLVMReturnStatusAction, &llvm_error)) {
  936. gb_printf_err("LLVM Error:\n%s\n", llvm_error);
  937. if (build_context.keep_temp_files) {
  938. TIME_SECTION("LLVM Print Module to File");
  939. String filepath_ll = lb_filepath_ll_for_module(m);
  940. if (LLVMPrintModuleToFile(m->mod, cast(char const *)filepath_ll.text, &llvm_error)) {
  941. gb_printf_err("LLVM Error: %s\n", llvm_error);
  942. exit_with_errors();
  943. return false;
  944. }
  945. }
  946. exit_with_errors();
  947. return 1;
  948. }
  949. return 0;
  950. }
  951. gb_internal lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProcedure *objc_names, Array<lbGlobalVariable> &global_variables) { // Startup Runtime
  952. Type *proc_type = alloc_type_proc(nullptr, nullptr, 0, nullptr, 0, false, ProcCC_Odin);
  953. lbProcedure *p = lb_create_dummy_procedure(main_module, str_lit(LB_STARTUP_RUNTIME_PROC_NAME), proc_type);
  954. p->is_startup = true;
  955. lb_add_attribute_to_proc(p->module, p->value, "optnone");
  956. lb_add_attribute_to_proc(p->module, p->value, "noinline");
  957. lb_begin_procedure_body(p);
  958. lb_setup_type_info_data(main_module);
  959. if (objc_names) {
  960. LLVMBuildCall2(p->builder, lb_type_internal_for_procedures_raw(main_module, objc_names->type), objc_names->value, nullptr, 0, "");
  961. }
  962. for (auto &var : global_variables) {
  963. if (var.is_initialized) {
  964. continue;
  965. }
  966. lbModule *entity_module = main_module;
  967. Entity *e = var.decl->entity;
  968. GB_ASSERT(e->kind == Entity_Variable);
  969. e->code_gen_module = entity_module;
  970. Ast *init_expr = var.decl->init_expr;
  971. if (init_expr != nullptr) {
  972. lbValue init = lb_build_expr(p, init_expr);
  973. if (init.value == nullptr) {
  974. LLVMTypeRef global_type = llvm_addr_type(p->module, var.var);
  975. if (is_type_untyped_nil(init.type)) {
  976. LLVMSetInitializer(var.var.value, LLVMConstNull(global_type));
  977. var.is_initialized = true;
  978. if (e->Variable.is_rodata) {
  979. LLVMSetGlobalConstant(var.var.value, true);
  980. }
  981. continue;
  982. }
  983. GB_PANIC("Invalid init value, got %s", expr_to_string(init_expr));
  984. }
  985. if (is_type_any(e->type) || is_type_union(e->type)) {
  986. var.init = init;
  987. } else if (lb_is_const_or_global(init)) {
  988. if (!var.is_initialized) {
  989. if (is_type_proc(init.type)) {
  990. init.value = LLVMConstPointerCast(init.value, lb_type(p->module, init.type));
  991. }
  992. LLVMSetInitializer(var.var.value, init.value);
  993. var.is_initialized = true;
  994. if (e->Variable.is_rodata) {
  995. LLVMSetGlobalConstant(var.var.value, true);
  996. }
  997. continue;
  998. }
  999. } else {
  1000. var.init = init;
  1001. }
  1002. }
  1003. if (var.init.value != nullptr) {
  1004. GB_ASSERT(!var.is_initialized);
  1005. Type *t = type_deref(var.var.type);
  1006. if (is_type_any(t)) {
  1007. // NOTE(bill): Edge case for 'any' type
  1008. Type *var_type = default_type(var.init.type);
  1009. gbString var_name = gb_string_make(permanent_allocator(), "__$global_any::");
  1010. gbString e_str = string_canonical_entity_name(temporary_allocator(), e);
  1011. var_name = gb_string_append_length(var_name, e_str, gb_strlen(e_str));
  1012. lbAddr g = lb_add_global_generated_with_name(main_module, var_type, var.init, make_string_c(var_name));
  1013. lb_addr_store(p, g, var.init);
  1014. lbValue gp = lb_addr_get_ptr(p, g);
  1015. lbValue data = lb_emit_struct_ep(p, var.var, 0);
  1016. lbValue ti = lb_emit_struct_ep(p, var.var, 1);
  1017. lb_emit_store(p, data, lb_emit_conv(p, gp, t_rawptr));
  1018. lb_emit_store(p, ti, lb_type_info(p, var_type));
  1019. } else {
  1020. LLVMTypeRef vt = llvm_addr_type(p->module, var.var);
  1021. lbValue src0 = lb_emit_conv(p, var.init, t);
  1022. LLVMValueRef src = OdinLLVMBuildTransmute(p, src0.value, vt);
  1023. LLVMValueRef dst = var.var.value;
  1024. LLVMBuildStore(p->builder, src, dst);
  1025. }
  1026. var.is_initialized = true;
  1027. }
  1028. }
  1029. CheckerInfo *info = main_module->gen->info;
  1030. for (Entity *e : info->init_procedures) {
  1031. lbValue value = lb_find_procedure_value_from_entity(main_module, e);
  1032. lb_emit_call(p, value, {}, ProcInlining_none);
  1033. }
  1034. lb_end_procedure_body(p);
  1035. lb_verify_function(main_module, p);
  1036. return p;
  1037. }
  1038. gb_internal lbProcedure *lb_create_cleanup_runtime(lbModule *main_module) { // Cleanup Runtime
  1039. Type *proc_type = alloc_type_proc(nullptr, nullptr, 0, nullptr, 0, false, ProcCC_Odin);
  1040. lbProcedure *p = lb_create_dummy_procedure(main_module, str_lit(LB_CLEANUP_RUNTIME_PROC_NAME), proc_type);
  1041. p->is_startup = true;
  1042. lb_add_attribute_to_proc(p->module, p->value, "optnone");
  1043. lb_add_attribute_to_proc(p->module, p->value, "noinline");
  1044. lb_begin_procedure_body(p);
  1045. CheckerInfo *info = main_module->gen->info;
  1046. for (Entity *e : info->fini_procedures) {
  1047. lbValue value = lb_find_procedure_value_from_entity(main_module, e);
  1048. lb_emit_call(p, value, {}, ProcInlining_none);
  1049. }
  1050. lb_end_procedure_body(p);
  1051. lb_verify_function(main_module, p);
  1052. return p;
  1053. }
  1054. gb_internal WORKER_TASK_PROC(lb_generate_procedures_and_types_per_module) {
  1055. lbModule *m = cast(lbModule *)data;
  1056. for (Entity *e : m->global_types_to_create) {
  1057. (void)lb_get_entity_name(m, e);
  1058. (void)lb_type(m, e->type);
  1059. }
  1060. for (Entity *e : m->global_procedures_to_create) {
  1061. (void)lb_get_entity_name(m, e);
  1062. array_add(&m->procedures_to_generate, lb_create_procedure(m, e));
  1063. }
  1064. return 0;
  1065. }
  1066. gb_internal GB_COMPARE_PROC(llvm_global_entity_cmp) {
  1067. Entity *x = *cast(Entity **)a;
  1068. Entity *y = *cast(Entity **)b;
  1069. if (x == y) {
  1070. return 0;
  1071. }
  1072. if (x->kind != y->kind) {
  1073. return cast(i32)(x->kind - y->kind);
  1074. }
  1075. i32 cmp = 0;
  1076. cmp = token_pos_cmp(x->token.pos, y->token.pos);
  1077. if (!cmp) {
  1078. return cmp;
  1079. }
  1080. return cmp;
  1081. }
  1082. gb_internal void lb_create_global_procedures_and_types(lbGenerator *gen, CheckerInfo *info, bool do_threading) {
  1083. auto *min_dep_set = &info->minimum_dependency_set;
  1084. for (Entity *e : info->entities) {
  1085. String name = e->token.string;
  1086. Scope * scope = e->scope;
  1087. if ((scope->flags & ScopeFlag_File) == 0) {
  1088. continue;
  1089. }
  1090. Scope *package_scope = scope->parent;
  1091. GB_ASSERT(package_scope->flags & ScopeFlag_Pkg);
  1092. switch (e->kind) {
  1093. case Entity_Variable:
  1094. // NOTE(bill): Handled above as it requires a specific load order
  1095. continue;
  1096. case Entity_ProcGroup:
  1097. continue;
  1098. case Entity_TypeName:
  1099. case Entity_Procedure:
  1100. break;
  1101. case Entity_Constant:
  1102. if (build_context.ODIN_DEBUG) {
  1103. add_debug_info_for_global_constant_from_entity(gen, e);
  1104. }
  1105. break;
  1106. }
  1107. bool polymorphic_struct = false;
  1108. if (e->type != nullptr && e->kind == Entity_TypeName) {
  1109. Type *bt = base_type(e->type);
  1110. if (bt->kind == Type_Struct) {
  1111. polymorphic_struct = is_type_polymorphic(bt);
  1112. }
  1113. }
  1114. if (!polymorphic_struct && !ptr_set_exists(min_dep_set, e)) {
  1115. // NOTE(bill): Nothing depends upon it so doesn't need to be built
  1116. continue;
  1117. }
  1118. lbModule *m = &gen->default_module;
  1119. if (USE_SEPARATE_MODULES) {
  1120. m = lb_module_of_entity(gen, e);
  1121. }
  1122. GB_ASSERT(m != nullptr);
  1123. if (e->kind == Entity_Procedure) {
  1124. array_add(&m->global_procedures_to_create, e);
  1125. } else if (e->kind == Entity_TypeName) {
  1126. array_add(&m->global_types_to_create, e);
  1127. }
  1128. }
  1129. for (auto const &entry : gen->modules) {
  1130. lbModule *m = entry.value;
  1131. array_sort(m->global_types_to_create, llvm_global_entity_cmp);
  1132. array_sort(m->global_procedures_to_create, llvm_global_entity_cmp);
  1133. }
  1134. if (do_threading) {
  1135. for (auto const &entry : gen->modules) {
  1136. lbModule *m = entry.value;
  1137. thread_pool_add_task(lb_generate_procedures_and_types_per_module, m);
  1138. }
  1139. } else {
  1140. for (auto const &entry : gen->modules) {
  1141. lbModule *m = entry.value;
  1142. lb_generate_procedures_and_types_per_module(m);
  1143. }
  1144. }
  1145. thread_pool_wait();
  1146. }
  1147. gb_internal void lb_generate_procedure(lbModule *m, lbProcedure *p);
  1148. gb_internal bool lb_is_module_empty(lbModule *m) {
  1149. if (LLVMGetFirstFunction(m->mod) == nullptr &&
  1150. LLVMGetFirstGlobal(m->mod) == nullptr) {
  1151. return true;
  1152. }
  1153. for (auto fn = LLVMGetFirstFunction(m->mod); fn != nullptr; fn = LLVMGetNextFunction(fn)) {
  1154. if (LLVMGetFirstBasicBlock(fn) != nullptr) {
  1155. return false;
  1156. }
  1157. }
  1158. for (auto g = LLVMGetFirstGlobal(m->mod); g != nullptr; g = LLVMGetNextGlobal(g)) {
  1159. LLVMLinkage linkage = LLVMGetLinkage(g);
  1160. if (linkage == LLVMExternalLinkage ||
  1161. linkage == LLVMWeakAnyLinkage) {
  1162. continue;
  1163. }
  1164. if (!LLVMIsExternallyInitialized(g)) {
  1165. return false;
  1166. }
  1167. }
  1168. return true;
  1169. }
  1170. struct lbLLVMEmitWorker {
  1171. LLVMTargetMachineRef target_machine;
  1172. LLVMCodeGenFileType code_gen_file_type;
  1173. String filepath_obj;
  1174. lbModule *m;
  1175. };
  1176. gb_internal WORKER_TASK_PROC(lb_llvm_emit_worker_proc) {
  1177. GB_ASSERT(MULTITHREAD_OBJECT_GENERATION);
  1178. char *llvm_error = nullptr;
  1179. auto wd = cast(lbLLVMEmitWorker *)data;
  1180. if (LLVMTargetMachineEmitToFile(wd->target_machine, wd->m->mod, cast(char *)wd->filepath_obj.text, wd->code_gen_file_type, &llvm_error)) {
  1181. gb_printf_err("LLVM Error: %s\n", llvm_error);
  1182. exit_with_errors();
  1183. }
  1184. debugf("Generated File: %.*s\n", LIT(wd->filepath_obj));
  1185. return 0;
  1186. }
  1187. gb_internal void lb_llvm_function_pass_per_function_internal(lbModule *module, lbProcedure *p, lbFunctionPassManagerKind pass_manager_kind = lbFunctionPassManager_default) {
  1188. LLVMPassManagerRef pass_manager = module->function_pass_managers[pass_manager_kind];
  1189. lb_run_function_pass_manager(pass_manager, p, pass_manager_kind);
  1190. }
  1191. gb_internal WORKER_TASK_PROC(lb_llvm_function_pass_per_module) {
  1192. lbModule *m = cast(lbModule *)data;
  1193. {
  1194. GB_ASSERT(m->function_pass_managers[lbFunctionPassManager_default] == nullptr);
  1195. for (i32 i = 0; i < lbFunctionPassManager_COUNT; i++) {
  1196. m->function_pass_managers[i] = LLVMCreateFunctionPassManagerForModule(m->mod);
  1197. }
  1198. for (i32 i = 0; i < lbFunctionPassManager_COUNT; i++) {
  1199. LLVMInitializeFunctionPassManager(m->function_pass_managers[i]);
  1200. }
  1201. lb_populate_function_pass_manager(m, m->function_pass_managers[lbFunctionPassManager_default], false, build_context.optimization_level);
  1202. lb_populate_function_pass_manager(m, m->function_pass_managers[lbFunctionPassManager_default_without_memcpy], true, build_context.optimization_level);
  1203. lb_populate_function_pass_manager_specific(m, m->function_pass_managers[lbFunctionPassManager_none], -1);
  1204. for (i32 i = 0; i < lbFunctionPassManager_COUNT; i++) {
  1205. LLVMFinalizeFunctionPassManager(m->function_pass_managers[i]);
  1206. }
  1207. }
  1208. if (m == &m->gen->default_module) {
  1209. lb_llvm_function_pass_per_function_internal(m, m->gen->startup_runtime);
  1210. lb_llvm_function_pass_per_function_internal(m, m->gen->cleanup_runtime);
  1211. lb_llvm_function_pass_per_function_internal(m, m->gen->objc_names);
  1212. }
  1213. for (lbProcedure *p : m->procedures_to_generate) {
  1214. if (p->body != nullptr) { // Build Procedure
  1215. lbFunctionPassManagerKind pass_manager_kind = lbFunctionPassManager_default;
  1216. if (p->flags & lbProcedureFlag_WithoutMemcpyPass) {
  1217. pass_manager_kind = lbFunctionPassManager_default_without_memcpy;
  1218. lb_add_attribute_to_proc(p->module, p->value, "optnone");
  1219. lb_add_attribute_to_proc(p->module, p->value, "noinline");
  1220. } else {
  1221. if (p->entity && p->entity->kind == Entity_Procedure) {
  1222. switch (p->entity->Procedure.optimization_mode) {
  1223. case ProcedureOptimizationMode_None:
  1224. pass_manager_kind = lbFunctionPassManager_none;
  1225. GB_ASSERT(lb_proc_has_attribute(p->module, p->value, "optnone"));
  1226. GB_ASSERT(lb_proc_has_attribute(p->module, p->value, "noinline"));
  1227. break;
  1228. case ProcedureOptimizationMode_FavorSize:
  1229. GB_ASSERT(lb_proc_has_attribute(p->module, p->value, "optsize"));
  1230. break;
  1231. }
  1232. }
  1233. }
  1234. lb_llvm_function_pass_per_function_internal(m, p, pass_manager_kind);
  1235. }
  1236. }
  1237. for (auto const &entry : m->equal_procs) {
  1238. lbProcedure *p = entry.value;
  1239. lb_llvm_function_pass_per_function_internal(m, p);
  1240. }
  1241. for (auto const &entry : m->hasher_procs) {
  1242. lbProcedure *p = entry.value;
  1243. lb_llvm_function_pass_per_function_internal(m, p);
  1244. }
  1245. for (auto const &entry : m->map_get_procs) {
  1246. lbProcedure *p = entry.value;
  1247. lb_llvm_function_pass_per_function_internal(m, p, lbFunctionPassManager_none);
  1248. }
  1249. for (auto const &entry : m->map_set_procs) {
  1250. lbProcedure *p = entry.value;
  1251. lb_llvm_function_pass_per_function_internal(m, p, lbFunctionPassManager_none);
  1252. }
  1253. return 0;
  1254. }
  1255. struct lbLLVMModulePassWorkerData {
  1256. lbModule *m;
  1257. LLVMTargetMachineRef target_machine;
  1258. };
  1259. gb_internal WORKER_TASK_PROC(lb_llvm_module_pass_worker_proc) {
  1260. auto wd = cast(lbLLVMModulePassWorkerData *)data;
  1261. lb_run_remove_unused_function_pass(wd->m);
  1262. lb_run_remove_unused_globals_pass(wd->m);
  1263. LLVMPassManagerRef module_pass_manager = LLVMCreatePassManager();
  1264. lb_populate_module_pass_manager(wd->target_machine, module_pass_manager, build_context.optimization_level);
  1265. LLVMRunPassManager(module_pass_manager, wd->m->mod);
  1266. #if LB_USE_NEW_PASS_SYSTEM
  1267. auto passes = array_make<char const *>(heap_allocator(), 0, 64);
  1268. defer (array_free(&passes));
  1269. LLVMPassBuilderOptionsRef pb_options = LLVMCreatePassBuilderOptions();
  1270. defer (LLVMDisposePassBuilderOptions(pb_options));
  1271. switch (build_context.optimization_level) {
  1272. case -1:
  1273. array_add(&passes, "function(annotation-remarks)");
  1274. break;
  1275. case 0:
  1276. array_add(&passes, "always-inline");
  1277. array_add(&passes, "function(annotation-remarks)");
  1278. break;
  1279. case 1:
  1280. // default<Os>
  1281. // Passes removed: coro, openmp, sroa
  1282. #if LLVM_VERSION_MAJOR == 17
  1283. array_add(&passes, u8R"(
  1284. annotation2metadata,
  1285. forceattrs,
  1286. inferattrs,
  1287. function<eager-inv>(
  1288. lower-expect,
  1289. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;no-switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1290. early-cse<>
  1291. ),
  1292. ipsccp,
  1293. called-value-propagation,
  1294. globalopt,
  1295. function<eager-inv>(
  1296. mem2reg,
  1297. instcombine<max-iterations=1000;no-use-loop-info>,
  1298. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>
  1299. ),
  1300. require<globals-aa>,
  1301. function(
  1302. invalidate<aa>
  1303. ),
  1304. require<profile-summary>,
  1305. cgscc(
  1306. devirt<4>(
  1307. inline<only-mandatory>,
  1308. inline,
  1309. function-attrs<skip-non-recursive>,
  1310. function<eager-inv;no-rerun>(
  1311. early-cse<memssa>,
  1312. speculative-execution,
  1313. jump-threading,
  1314. correlated-propagation,
  1315. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1316. instcombine<max-iterations=1000;no-use-loop-info>,
  1317. aggressive-instcombine,
  1318. constraint-elimination,
  1319. tailcallelim,
  1320. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1321. reassociate,
  1322. loop-mssa(
  1323. loop-instsimplify,
  1324. loop-simplifycfg,
  1325. licm<no-allowspeculation>,
  1326. loop-rotate<header-duplication;no-prepare-for-lto>,
  1327. licm<allowspeculation>,
  1328. simple-loop-unswitch<no-nontrivial;trivial>
  1329. ),
  1330. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1331. instcombine<max-iterations=1000;no-use-loop-info>,
  1332. loop(
  1333. loop-idiom,
  1334. indvars,
  1335. loop-deletion,
  1336. loop-unroll-full
  1337. ),
  1338. vector-combine,
  1339. mldst-motion<no-split-footer-bb>,
  1340. gvn<>,
  1341. sccp,
  1342. bdce,
  1343. instcombine<max-iterations=1000;no-use-loop-info>,
  1344. jump-threading,
  1345. correlated-propagation,
  1346. adce,
  1347. memcpyopt,
  1348. dse,
  1349. move-auto-init,
  1350. loop-mssa(
  1351. licm<allowspeculation>
  1352. ),
  1353. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;hoist-common-insts;sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1354. instcombine<max-iterations=1000;no-use-loop-info>
  1355. ),
  1356. function-attrs,
  1357. function(
  1358. require<should-not-run-function-passes>
  1359. )
  1360. )
  1361. ),
  1362. deadargelim,
  1363. globalopt,
  1364. globaldce,
  1365. elim-avail-extern,
  1366. rpo-function-attrs,
  1367. recompute-globalsaa,
  1368. function<eager-inv>(
  1369. float2int,
  1370. lower-constant-intrinsics,
  1371. loop(
  1372. loop-rotate<header-duplication;no-prepare-for-lto>,
  1373. loop-deletion
  1374. ),
  1375. loop-distribute,
  1376. inject-tli-mappings,
  1377. loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>,
  1378. loop-load-elim,
  1379. instcombine<max-iterations=1000;no-use-loop-info>,
  1380. simplifycfg<bonus-inst-threshold=1;forward-switch-cond;switch-range-to-icmp;switch-to-lookup;no-keep-loops;hoist-common-insts;sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1381. slp-vectorizer,
  1382. vector-combine,
  1383. instcombine<max-iterations=1000;no-use-loop-info>,
  1384. loop-unroll<O2>,
  1385. transform-warning,
  1386. instcombine<max-iterations=1000;no-use-loop-info>,
  1387. loop-mssa(
  1388. licm<allowspeculation>
  1389. ),
  1390. alignment-from-assumptions,
  1391. loop-sink,
  1392. instsimplify,
  1393. div-rem-pairs,
  1394. tailcallelim,
  1395. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>
  1396. ),
  1397. globaldce,
  1398. constmerge,
  1399. cg-profile,
  1400. rel-lookup-table-converter,
  1401. function(
  1402. annotation-remarks
  1403. ),
  1404. verify
  1405. )");
  1406. #else
  1407. array_add(&passes, u8R"(
  1408. annotation2metadata,
  1409. forceattrs,
  1410. inferattrs,
  1411. function<eager-inv>(
  1412. lower-expect,
  1413. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;no-switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1414. sroa<modify-cfg>,
  1415. early-cse<>
  1416. ),
  1417. ipsccp,
  1418. called-value-propagation,
  1419. globalopt,
  1420. function<eager-inv>(
  1421. mem2reg,
  1422. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1423. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>
  1424. ),
  1425. always-inline,
  1426. require<globals-aa>,
  1427. function(
  1428. invalidate<aa>
  1429. ),
  1430. require<profile-summary>,
  1431. cgscc(
  1432. devirt<4>(
  1433. inline,
  1434. function-attrs<skip-non-recursive-function-attrs>,
  1435. function<eager-inv;no-rerun>(
  1436. sroa<modify-cfg>,
  1437. early-cse<memssa>,
  1438. speculative-execution<only-if-divergent-target>,
  1439. jump-threading,
  1440. correlated-propagation,
  1441. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1442. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1443. aggressive-instcombine,
  1444. tailcallelim,
  1445. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1446. reassociate,
  1447. constraint-elimination,
  1448. loop-mssa(
  1449. loop-instsimplify,
  1450. loop-simplifycfg,
  1451. licm<no-allowspeculation>,
  1452. loop-rotate<header-duplication;no-prepare-for-lto>,
  1453. licm<allowspeculation>,
  1454. simple-loop-unswitch<no-nontrivial;trivial>
  1455. ),
  1456. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1457. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1458. loop(
  1459. loop-idiom,
  1460. indvars,
  1461. loop-deletion,
  1462. loop-unroll-full
  1463. ),
  1464. sroa<modify-cfg>,
  1465. vector-combine,
  1466. mldst-motion<no-split-footer-bb>,
  1467. gvn<>,
  1468. sccp,
  1469. bdce,
  1470. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1471. jump-threading,
  1472. correlated-propagation,
  1473. adce,
  1474. memcpyopt,
  1475. dse,
  1476. move-auto-init,
  1477. loop-mssa(
  1478. licm<allowspeculation>
  1479. ),
  1480. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;hoist-common-insts;sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1481. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>
  1482. ),
  1483. function-attrs,
  1484. function(
  1485. require<should-not-run-function-passes>
  1486. )
  1487. )
  1488. ),
  1489. deadargelim,
  1490. globalopt,
  1491. globaldce,
  1492. elim-avail-extern,
  1493. rpo-function-attrs,
  1494. recompute-globalsaa,
  1495. function<eager-inv>(
  1496. float2int,
  1497. lower-constant-intrinsics,
  1498. loop(
  1499. loop-rotate<header-duplication;no-prepare-for-lto>,
  1500. loop-deletion
  1501. ),
  1502. loop-distribute,
  1503. inject-tli-mappings,
  1504. loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>,
  1505. infer-alignment,
  1506. loop-load-elim,
  1507. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1508. simplifycfg<bonus-inst-threshold=1;forward-switch-cond;switch-range-to-icmp;switch-to-lookup;no-keep-loops;hoist-common-insts;sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1509. slp-vectorizer,
  1510. vector-combine,
  1511. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1512. loop-unroll<O2>,
  1513. transform-warning,
  1514. sroa<preserve-cfg>,
  1515. infer-alignment,
  1516. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1517. loop-mssa(
  1518. licm<allowspeculation>
  1519. ),
  1520. alignment-from-assumptions,
  1521. loop-sink,
  1522. instsimplify,
  1523. div-rem-pairs,
  1524. tailcallelim,
  1525. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>
  1526. ),
  1527. globaldce,
  1528. constmerge,
  1529. cg-profile,
  1530. rel-lookup-table-converter,
  1531. function(
  1532. annotation-remarks
  1533. ),
  1534. verify
  1535. )");
  1536. #endif
  1537. break;
  1538. // default<O2>
  1539. // Passes removed: coro, openmp, sroa
  1540. case 2:
  1541. #if LLVM_VERSION_MAJOR == 17
  1542. array_add(&passes, u8R"(
  1543. annotation2metadata,
  1544. forceattrs,
  1545. inferattrs,
  1546. function<eager-inv>(
  1547. lower-expect,
  1548. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;no-switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1549. early-cse<>
  1550. ),
  1551. ipsccp,
  1552. called-value-propagation,
  1553. globalopt,
  1554. function<eager-inv>(
  1555. mem2reg,
  1556. instcombine<max-iterations=1000;no-use-loop-info>,
  1557. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>
  1558. ),
  1559. require<globals-aa>,
  1560. function(
  1561. invalidate<aa>
  1562. ),
  1563. require<profile-summary>,
  1564. cgscc(
  1565. devirt<4>(
  1566. inline<only-mandatory>,
  1567. inline,
  1568. function-attrs<skip-non-recursive>,
  1569. function<eager-inv;no-rerun>(
  1570. early-cse<memssa>,
  1571. speculative-execution,
  1572. jump-threading,
  1573. correlated-propagation,
  1574. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1575. instcombine<max-iterations=1000;no-use-loop-info>,
  1576. aggressive-instcombine,
  1577. constraint-elimination,
  1578. libcalls-shrinkwrap,
  1579. tailcallelim,
  1580. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1581. reassociate,
  1582. loop-mssa(
  1583. loop-instsimplify,
  1584. loop-simplifycfg,
  1585. licm<no-allowspeculation>,
  1586. loop-rotate<header-duplication;no-prepare-for-lto>,
  1587. licm<allowspeculation>,
  1588. simple-loop-unswitch<no-nontrivial;trivial>
  1589. ),
  1590. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1591. instcombine<max-iterations=1000;no-use-loop-info>,
  1592. loop(
  1593. loop-idiom,
  1594. indvars,
  1595. loop-deletion,
  1596. loop-unroll-full
  1597. ),
  1598. vector-combine,
  1599. mldst-motion<no-split-footer-bb>,
  1600. gvn<>,
  1601. sccp,
  1602. bdce,
  1603. instcombine<max-iterations=1000;no-use-loop-info>,
  1604. jump-threading,
  1605. correlated-propagation,
  1606. adce,
  1607. memcpyopt,
  1608. dse,
  1609. move-auto-init,
  1610. loop-mssa(
  1611. licm<allowspeculation>
  1612. ),
  1613. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;hoist-common-insts;sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1614. instcombine<max-iterations=1000;no-use-loop-info>
  1615. ),
  1616. function-attrs,
  1617. function(
  1618. require<should-not-run-function-passes>
  1619. )
  1620. )
  1621. ),
  1622. deadargelim,
  1623. globalopt,
  1624. globaldce,
  1625. elim-avail-extern,
  1626. rpo-function-attrs,
  1627. recompute-globalsaa,
  1628. function<eager-inv>(
  1629. float2int,
  1630. lower-constant-intrinsics,
  1631. loop(
  1632. loop-rotate<header-duplication;no-prepare-for-lto>,
  1633. loop-deletion
  1634. ),
  1635. loop-distribute,
  1636. inject-tli-mappings,
  1637. loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>,
  1638. loop-load-elim,
  1639. instcombine<max-iterations=1000;no-use-loop-info>,
  1640. simplifycfg<bonus-inst-threshold=1;forward-switch-cond;switch-range-to-icmp;switch-to-lookup;no-keep-loops;hoist-common-insts;sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1641. slp-vectorizer,
  1642. vector-combine,
  1643. instcombine<max-iterations=1000;no-use-loop-info>,
  1644. loop-unroll<O2>,
  1645. transform-warning,
  1646. instcombine<max-iterations=1000;no-use-loop-info>,
  1647. loop-mssa(
  1648. licm<allowspeculation>
  1649. ),
  1650. alignment-from-assumptions,
  1651. loop-sink,
  1652. instsimplify,
  1653. div-rem-pairs,
  1654. tailcallelim,
  1655. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>
  1656. ),
  1657. globaldce,
  1658. constmerge,
  1659. cg-profile,
  1660. rel-lookup-table-converter,
  1661. function(
  1662. annotation-remarks
  1663. ),
  1664. verify
  1665. )");
  1666. #else
  1667. array_add(&passes, u8R"(
  1668. annotation2metadata,
  1669. forceattrs,
  1670. inferattrs,
  1671. function<eager-inv>(
  1672. lower-expect,
  1673. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;no-switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1674. sroa<modify-cfg>,
  1675. early-cse<>
  1676. ),
  1677. ipsccp,
  1678. called-value-propagation,
  1679. globalopt,
  1680. function<eager-inv>(
  1681. mem2reg,
  1682. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1683. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>
  1684. ),
  1685. always-inline,
  1686. require<globals-aa>,
  1687. function(
  1688. invalidate<aa>
  1689. ),
  1690. require<profile-summary>,
  1691. cgscc(
  1692. devirt<4>(
  1693. inline,
  1694. function-attrs<skip-non-recursive-function-attrs>,
  1695. function<eager-inv;no-rerun>(
  1696. sroa<modify-cfg>,
  1697. early-cse<memssa>,
  1698. speculative-execution<only-if-divergent-target>,
  1699. jump-threading,
  1700. correlated-propagation,
  1701. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1702. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1703. aggressive-instcombine,
  1704. libcalls-shrinkwrap,
  1705. tailcallelim,
  1706. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1707. reassociate,
  1708. constraint-elimination,
  1709. loop-mssa(
  1710. loop-instsimplify,
  1711. loop-simplifycfg,
  1712. licm<no-allowspeculation>,
  1713. loop-rotate<header-duplication;no-prepare-for-lto>,
  1714. licm<allowspeculation>,
  1715. simple-loop-unswitch<no-nontrivial;trivial>
  1716. ),
  1717. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1718. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1719. loop(
  1720. loop-idiom,
  1721. indvars,
  1722. loop-deletion,
  1723. loop-unroll-full
  1724. ),
  1725. sroa<modify-cfg>,
  1726. vector-combine,
  1727. mldst-motion<no-split-footer-bb>,
  1728. gvn<>,
  1729. sccp,
  1730. bdce,
  1731. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1732. jump-threading,
  1733. correlated-propagation,
  1734. adce,
  1735. memcpyopt,
  1736. dse,
  1737. move-auto-init,
  1738. loop-mssa(
  1739. licm<allowspeculation>
  1740. ),
  1741. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;hoist-common-insts;sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1742. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>
  1743. ),
  1744. function-attrs,
  1745. function(
  1746. require<should-not-run-function-passes>
  1747. )
  1748. )
  1749. ),
  1750. deadargelim,
  1751. globalopt,
  1752. globaldce,
  1753. elim-avail-extern,
  1754. rpo-function-attrs,
  1755. recompute-globalsaa,
  1756. function<eager-inv>(
  1757. float2int,
  1758. lower-constant-intrinsics,
  1759. loop(
  1760. loop-rotate<header-duplication;no-prepare-for-lto>,
  1761. loop-deletion
  1762. ),
  1763. loop-distribute,
  1764. inject-tli-mappings,
  1765. loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>,
  1766. infer-alignment,
  1767. loop-load-elim,
  1768. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1769. simplifycfg<bonus-inst-threshold=1;forward-switch-cond;switch-range-to-icmp;switch-to-lookup;no-keep-loops;hoist-common-insts;sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1770. slp-vectorizer,
  1771. vector-combine,
  1772. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1773. loop-unroll<O2>,
  1774. transform-warning,
  1775. sroa<modify-cfg>,
  1776. infer-alignment,
  1777. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1778. loop-mssa(
  1779. licm<allowspeculation>
  1780. ),
  1781. alignment-from-assumptions,
  1782. loop-sink,
  1783. instsimplify,
  1784. div-rem-pairs,
  1785. tailcallelim,
  1786. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>
  1787. ),
  1788. globaldce,
  1789. constmerge,
  1790. cg-profile,
  1791. rel-lookup-table-converter,
  1792. function(
  1793. annotation-remarks
  1794. ),
  1795. verify
  1796. )");
  1797. #endif
  1798. break;
  1799. case 3:
  1800. // default<O3>
  1801. // Passes removed: coro, openmp, sroa
  1802. #if LLVM_VERSION_MAJOR == 17
  1803. array_add(&passes, u8R"(
  1804. annotation2metadata,
  1805. forceattrs,
  1806. inferattrs,
  1807. function<eager-inv>(
  1808. lower-expect,
  1809. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;no-switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1810. early-cse<>,
  1811. callsite-splitting
  1812. ),
  1813. ipsccp,
  1814. called-value-propagation,
  1815. globalopt,
  1816. function<eager-inv>(
  1817. mem2reg,
  1818. instcombine<max-iterations=1000;no-use-loop-info>,
  1819. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>
  1820. ),
  1821. require<globals-aa>,
  1822. function(
  1823. invalidate<aa>
  1824. ),
  1825. require<profile-summary>,
  1826. cgscc(
  1827. devirt<4>(
  1828. inline<only-mandatory>,
  1829. inline,
  1830. function-attrs<skip-non-recursive>,
  1831. argpromotion,
  1832. function<eager-inv;no-rerun>(
  1833. early-cse<memssa>,
  1834. speculative-execution,
  1835. jump-threading,
  1836. correlated-propagation,
  1837. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1838. instcombine<max-iterations=1000;no-use-loop-info>,
  1839. aggressive-instcombine,
  1840. constraint-elimination,
  1841. libcalls-shrinkwrap,
  1842. tailcallelim,
  1843. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1844. reassociate,
  1845. loop-mssa(
  1846. loop-instsimplify,
  1847. loop-simplifycfg,
  1848. licm<no-allowspeculation>,
  1849. loop-rotate<header-duplication;no-prepare-for-lto>,
  1850. licm<allowspeculation>,
  1851. simple-loop-unswitch<nontrivial;trivial>
  1852. ),
  1853. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1854. instcombine<max-iterations=1000;no-use-loop-info>,
  1855. loop(
  1856. loop-idiom,
  1857. indvars,
  1858. loop-deletion,
  1859. loop-unroll-full
  1860. ),
  1861. vector-combine,
  1862. mldst-motion<no-split-footer-bb>,
  1863. gvn<>,
  1864. sccp,
  1865. bdce,
  1866. instcombine<max-iterations=1000;no-use-loop-info>,
  1867. jump-threading,
  1868. correlated-propagation,
  1869. adce,
  1870. memcpyopt,
  1871. dse,
  1872. move-auto-init,
  1873. loop-mssa(
  1874. licm<allowspeculation>
  1875. ),
  1876. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;hoist-common-insts;sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1877. instcombine<max-iterations=1000;no-use-loop-info>
  1878. ),
  1879. function-attrs,
  1880. function(
  1881. require<should-not-run-function-passes>
  1882. )
  1883. )
  1884. ),
  1885. deadargelim,
  1886. globalopt,
  1887. globaldce,
  1888. elim-avail-extern,
  1889. rpo-function-attrs,
  1890. recompute-globalsaa,
  1891. function<eager-inv>(
  1892. float2int,
  1893. lower-constant-intrinsics,
  1894. chr,
  1895. loop(
  1896. loop-rotate<header-duplication;no-prepare-for-lto>,
  1897. loop-deletion
  1898. ),
  1899. loop-distribute,
  1900. inject-tli-mappings,
  1901. loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>,
  1902. loop-load-elim,
  1903. instcombine<max-iterations=1000;no-use-loop-info>,
  1904. simplifycfg<bonus-inst-threshold=1;forward-switch-cond;switch-range-to-icmp;switch-to-lookup;no-keep-loops;hoist-common-insts;sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1905. slp-vectorizer,
  1906. vector-combine,
  1907. instcombine<max-iterations=1000;no-use-loop-info>,
  1908. loop-unroll<O3>,
  1909. transform-warning,
  1910. instcombine<max-iterations=1000;no-use-loop-info>,
  1911. loop-mssa(
  1912. licm<allowspeculation>
  1913. ),
  1914. alignment-from-assumptions,
  1915. loop-sink,
  1916. instsimplify,
  1917. div-rem-pairs,
  1918. tailcallelim,
  1919. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>
  1920. ),
  1921. globaldce,
  1922. constmerge,
  1923. cg-profile,
  1924. rel-lookup-table-converter,
  1925. function(
  1926. annotation-remarks
  1927. ),
  1928. verify
  1929. )");
  1930. #else
  1931. array_add(&passes, u8R"(
  1932. annotation2metadata,
  1933. forceattrs,
  1934. inferattrs,
  1935. function<eager-inv>(
  1936. lower-expect,
  1937. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;no-switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1938. sroa<modify-cfg>,
  1939. early-cse<>,
  1940. callsite-splitting
  1941. ),
  1942. ipsccp,
  1943. called-value-propagation,
  1944. globalopt,
  1945. function<eager-inv>(
  1946. mem2reg,
  1947. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1948. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>
  1949. ),
  1950. always-inline,
  1951. require<globals-aa>,
  1952. function(invalidate<aa>),
  1953. require<profile-summary>,
  1954. cgscc(
  1955. devirt<4>(
  1956. inline,
  1957. function-attrs<skip-non-recursive-function-attrs>,
  1958. argpromotion,
  1959. function<eager-inv;no-rerun>(
  1960. sroa<modify-cfg>,
  1961. early-cse<memssa>,
  1962. speculative-execution<only-if-divergent-target>,
  1963. jump-threading,
  1964. correlated-propagation,
  1965. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1966. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1967. aggressive-instcombine,
  1968. libcalls-shrinkwrap,
  1969. tailcallelim,
  1970. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1971. reassociate,
  1972. constraint-elimination,
  1973. loop-mssa(
  1974. loop-instsimplify,
  1975. loop-simplifycfg,
  1976. licm<no-allowspeculation>,
  1977. loop-rotate<header-duplication;no-prepare-for-lto>,
  1978. licm<allowspeculation>,
  1979. simple-loop-unswitch<nontrivial;trivial>
  1980. ),
  1981. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>,
  1982. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1983. loop(
  1984. loop-idiom,
  1985. indvars,
  1986. loop-deletion,
  1987. loop-unroll-full
  1988. ),
  1989. sroa<modify-cfg>,
  1990. vector-combine,
  1991. mldst-motion<no-split-footer-bb>,
  1992. gvn<>,
  1993. sccp,
  1994. bdce,
  1995. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  1996. jump-threading,
  1997. correlated-propagation,
  1998. adce,
  1999. memcpyopt,
  2000. dse,
  2001. move-auto-init,
  2002. loop-mssa(licm<allowspeculation>),
  2003. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;hoist-common-insts;sink-common-insts;speculate-blocks;simplify-cond-branch>,
  2004. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>
  2005. ),
  2006. function-attrs,
  2007. function(
  2008. require<should-not-run-function-passes>
  2009. )
  2010. )
  2011. ),
  2012. deadargelim,
  2013. globalopt,
  2014. globaldce,
  2015. elim-avail-extern,
  2016. rpo-function-attrs,
  2017. recompute-globalsaa,
  2018. function<eager-inv>(
  2019. float2int,
  2020. lower-constant-intrinsics,
  2021. chr,
  2022. loop(
  2023. loop-rotate<header-duplication;no-prepare-for-lto>,
  2024. loop-deletion
  2025. ),
  2026. loop-distribute,
  2027. inject-tli-mappings,
  2028. loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>,
  2029. infer-alignment,
  2030. loop-load-elim,
  2031. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  2032. simplifycfg<bonus-inst-threshold=1;forward-switch-cond;switch-range-to-icmp;switch-to-lookup;no-keep-loops;hoist-common-insts;sink-common-insts;speculate-blocks;simplify-cond-branch>,
  2033. slp-vectorizer,
  2034. vector-combine,
  2035. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  2036. loop-unroll<O3>,
  2037. transform-warning,
  2038. sroa<preserve-cfg>,
  2039. infer-alignment,
  2040. instcombine<max-iterations=1;no-use-loop-info;no-verify-fixpoint>,
  2041. loop-mssa(licm<allowspeculation>),
  2042. alignment-from-assumptions,
  2043. loop-sink,
  2044. instsimplify,
  2045. div-rem-pairs,
  2046. tailcallelim,
  2047. simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-sink-common-insts;speculate-blocks;simplify-cond-branch>
  2048. ),
  2049. globaldce,
  2050. constmerge,
  2051. cg-profile,
  2052. rel-lookup-table-converter,
  2053. function(
  2054. annotation-remarks
  2055. ),
  2056. verify
  2057. )");
  2058. #endif
  2059. break;
  2060. }
  2061. // asan - Linux, Darwin, Windows
  2062. // msan - linux
  2063. // tsan - Linux, Darwin
  2064. // ubsan - Linux, Darwin, Windows (NOT SUPPORTED WITH LLVM C-API)
  2065. if (build_context.sanitizer_flags & SanitizerFlag_Address) {
  2066. array_add(&passes, "asan");
  2067. }
  2068. if (build_context.sanitizer_flags & SanitizerFlag_Memory) {
  2069. array_add(&passes, "msan");
  2070. }
  2071. if (build_context.sanitizer_flags & SanitizerFlag_Thread) {
  2072. array_add(&passes, "tsan");
  2073. }
  2074. if (passes.count == 0) {
  2075. array_add(&passes, "verify");
  2076. }
  2077. gbString passes_str = gb_string_make_reserve(heap_allocator(), 1024);
  2078. defer (gb_string_free(passes_str));
  2079. for_array(i, passes) {
  2080. if (i != 0) {
  2081. passes_str = gb_string_appendc(passes_str, ",");
  2082. }
  2083. passes_str = gb_string_appendc(passes_str, passes[i]);
  2084. }
  2085. for (isize i = 0; i < gb_string_length(passes_str); /**/) {
  2086. switch (passes_str[i]) {
  2087. case ' ':
  2088. case '\n':
  2089. case '\t':
  2090. gb_memmove(&passes_str[i], &passes_str[i+1], gb_string_length(passes_str)-i);
  2091. GB_STRING_HEADER(passes_str)->length -= 1;
  2092. continue;
  2093. default:
  2094. i += 1;
  2095. break;
  2096. }
  2097. }
  2098. LLVMErrorRef llvm_err = LLVMRunPasses(wd->m->mod, passes_str, wd->target_machine, pb_options);
  2099. defer (LLVMConsumeError(llvm_err));
  2100. if (llvm_err != nullptr) {
  2101. char *llvm_error = LLVMGetErrorMessage(llvm_err);
  2102. gb_printf_err("LLVM Error:\n%s\n", llvm_error);
  2103. LLVMDisposeErrorMessage(llvm_error);
  2104. llvm_error = nullptr;
  2105. if (build_context.keep_temp_files) {
  2106. TIME_SECTION("LLVM Print Module to File");
  2107. String filepath_ll = lb_filepath_ll_for_module(wd->m);
  2108. if (LLVMPrintModuleToFile(wd->m->mod, cast(char const *)filepath_ll.text, &llvm_error)) {
  2109. gb_printf_err("LLVM Error: %s\n", llvm_error);
  2110. }
  2111. }
  2112. exit_with_errors();
  2113. return 1;
  2114. }
  2115. #endif
  2116. return 0;
  2117. }
  2118. gb_internal WORKER_TASK_PROC(lb_generate_procedures_worker_proc) {
  2119. lbModule *m = cast(lbModule *)data;
  2120. for (isize i = 0; i < m->procedures_to_generate.count; i++) {
  2121. lbProcedure *p = m->procedures_to_generate[i];
  2122. lb_generate_procedure(p->module, p);
  2123. }
  2124. return 0;
  2125. }
  2126. gb_internal void lb_generate_procedures(lbGenerator *gen, bool do_threading) {
  2127. if (do_threading) {
  2128. for (auto const &entry : gen->modules) {
  2129. lbModule *m = entry.value;
  2130. thread_pool_add_task(lb_generate_procedures_worker_proc, m);
  2131. }
  2132. thread_pool_wait();
  2133. } else {
  2134. for (auto const &entry : gen->modules) {
  2135. lbModule *m = entry.value;
  2136. lb_generate_procedures_worker_proc(m);
  2137. }
  2138. }
  2139. }
  2140. gb_internal WORKER_TASK_PROC(lb_generate_missing_procedures_to_check_worker_proc) {
  2141. lbModule *m = cast(lbModule *)data;
  2142. for (isize i = 0; i < m->missing_procedures_to_check.count; i++) {
  2143. lbProcedure *p = m->missing_procedures_to_check[i];
  2144. debugf("Generate missing procedure: %.*s module %p\n", LIT(p->name), m);
  2145. lb_generate_procedure(m, p);
  2146. }
  2147. return 0;
  2148. }
  2149. gb_internal void lb_generate_missing_procedures(lbGenerator *gen, bool do_threading) {
  2150. if (do_threading) {
  2151. for (auto const &entry : gen->modules) {
  2152. lbModule *m = entry.value;
  2153. // NOTE(bill): procedures may be added during generation
  2154. thread_pool_add_task(lb_generate_missing_procedures_to_check_worker_proc, m);
  2155. }
  2156. thread_pool_wait();
  2157. } else {
  2158. for (auto const &entry : gen->modules) {
  2159. lbModule *m = entry.value;
  2160. // NOTE(bill): procedures may be added during generation
  2161. lb_generate_missing_procedures_to_check_worker_proc(m);
  2162. }
  2163. }
  2164. }
  2165. gb_internal void lb_debug_info_complete_types_and_finalize(lbGenerator *gen) {
  2166. for (auto const &entry : gen->modules) {
  2167. lbModule *m = entry.value;
  2168. if (m->debug_builder != nullptr) {
  2169. LLVMDIBuilderFinalize(m->debug_builder);
  2170. }
  2171. }
  2172. }
  2173. gb_internal void lb_llvm_function_passes(lbGenerator *gen, bool do_threading) {
  2174. if (do_threading) {
  2175. for (auto const &entry : gen->modules) {
  2176. lbModule *m = entry.value;
  2177. thread_pool_add_task(lb_llvm_function_pass_per_module, m);
  2178. }
  2179. thread_pool_wait();
  2180. } else {
  2181. for (auto const &entry : gen->modules) {
  2182. lbModule *m = entry.value;
  2183. lb_llvm_function_pass_per_module(m);
  2184. }
  2185. }
  2186. }
  2187. gb_internal void lb_llvm_module_passes(lbGenerator *gen, bool do_threading) {
  2188. if (do_threading) {
  2189. for (auto const &entry : gen->modules) {
  2190. lbModule *m = entry.value;
  2191. auto wd = gb_alloc_item(permanent_allocator(), lbLLVMModulePassWorkerData);
  2192. wd->m = m;
  2193. wd->target_machine = m->target_machine;
  2194. if (do_threading) {
  2195. thread_pool_add_task(lb_llvm_module_pass_worker_proc, wd);
  2196. } else {
  2197. lb_llvm_module_pass_worker_proc(wd);
  2198. }
  2199. }
  2200. thread_pool_wait();
  2201. } else {
  2202. for (auto const &entry : gen->modules) {
  2203. lbModule *m = entry.value;
  2204. auto wd = gb_alloc_item(permanent_allocator(), lbLLVMModulePassWorkerData);
  2205. wd->m = m;
  2206. wd->target_machine = m->target_machine;
  2207. lb_llvm_module_pass_worker_proc(wd);
  2208. }
  2209. }
  2210. }
  2211. gb_internal String lb_filepath_ll_for_module(lbModule *m) {
  2212. String path = concatenate3_strings(permanent_allocator(),
  2213. build_context.build_paths[BuildPath_Output].basename,
  2214. STR_LIT("/"),
  2215. build_context.build_paths[BuildPath_Output].name
  2216. );
  2217. if (m->file) {
  2218. char buf[32] = {};
  2219. isize n = gb_snprintf(buf, gb_size_of(buf), "-%u", m->file->id);
  2220. String suffix = make_string((u8 *)buf, n-1);
  2221. path = concatenate_strings(permanent_allocator(), path, suffix);
  2222. } else if (m->pkg) {
  2223. path = concatenate3_strings(permanent_allocator(), path, STR_LIT("-"), m->pkg->name);
  2224. } else if (USE_SEPARATE_MODULES) {
  2225. path = concatenate_strings(permanent_allocator(), path, STR_LIT("-builtin"));
  2226. }
  2227. path = concatenate_strings(permanent_allocator(), path, STR_LIT(".ll"));
  2228. return path;
  2229. }
  2230. gb_internal String lb_filepath_obj_for_module(lbModule *m) {
  2231. String basename = build_context.build_paths[BuildPath_Output].basename;
  2232. String name = build_context.build_paths[BuildPath_Output].name;
  2233. bool use_temporary_directory = false;
  2234. if (USE_SEPARATE_MODULES && build_context.build_mode == BuildMode_Executable) {
  2235. // NOTE(bill): use a temporary directory
  2236. String dir = temporary_directory(permanent_allocator());
  2237. if (dir.len != 0) {
  2238. basename = dir;
  2239. use_temporary_directory = true;
  2240. }
  2241. }
  2242. gbString path = gb_string_make_length(heap_allocator(), basename.text, basename.len);
  2243. path = gb_string_appendc(path, "/");
  2244. path = gb_string_append_length(path, name.text, name.len);
  2245. if (m->file) {
  2246. char buf[32] = {};
  2247. isize n = gb_snprintf(buf, gb_size_of(buf), "-%u", m->file->id);
  2248. String suffix = make_string((u8 *)buf, n-1);
  2249. path = gb_string_append_length(path, suffix.text, suffix.len);
  2250. } else if (m->pkg) {
  2251. path = gb_string_appendc(path, "-");
  2252. path = gb_string_append_length(path, m->pkg->name.text, m->pkg->name.len);
  2253. }
  2254. if (use_temporary_directory) {
  2255. path = gb_string_append_fmt(path, "-%p", m);
  2256. }
  2257. String ext = {};
  2258. if (build_context.build_mode == BuildMode_Assembly) {
  2259. ext = STR_LIT(".S");
  2260. } else {
  2261. if (is_arch_wasm()) {
  2262. ext = STR_LIT(".wasm.o");
  2263. } else {
  2264. switch (build_context.metrics.os) {
  2265. case TargetOs_windows:
  2266. ext = STR_LIT(".obj");
  2267. break;
  2268. default:
  2269. case TargetOs_darwin:
  2270. case TargetOs_linux:
  2271. case TargetOs_essence:
  2272. ext = STR_LIT(".o");
  2273. break;
  2274. case TargetOs_freestanding:
  2275. switch (build_context.metrics.abi) {
  2276. default:
  2277. case TargetABI_Default:
  2278. case TargetABI_SysV:
  2279. ext = STR_LIT(".o");
  2280. break;
  2281. case TargetABI_Win64:
  2282. ext = STR_LIT(".obj");
  2283. break;
  2284. }
  2285. break;
  2286. }
  2287. }
  2288. }
  2289. path = gb_string_append_length(path, ext.text, ext.len);
  2290. return make_string(cast(u8 *)path, gb_string_length(path));
  2291. }
  2292. gb_internal bool lb_llvm_module_verification(lbGenerator *gen, bool do_threading) {
  2293. if (LLVM_IGNORE_VERIFICATION) {
  2294. return true;
  2295. }
  2296. if (do_threading) {
  2297. for (auto const &entry : gen->modules) {
  2298. lbModule *m = entry.value;
  2299. thread_pool_add_task(lb_llvm_module_verification_worker_proc, m);
  2300. }
  2301. thread_pool_wait();
  2302. } else {
  2303. for (auto const &entry : gen->modules) {
  2304. lbModule *m = entry.value;
  2305. if (lb_llvm_module_verification_worker_proc(m)) {
  2306. return false;
  2307. }
  2308. }
  2309. }
  2310. return true;
  2311. }
  2312. gb_internal void lb_add_foreign_library_paths(lbGenerator *gen) {
  2313. for (auto const &entry : gen->modules) {
  2314. lbModule *m = entry.value;
  2315. for (Entity *e : m->info->required_foreign_imports_through_force) {
  2316. lb_add_foreign_library_path(m, e);
  2317. }
  2318. if (lb_is_module_empty(m)) {
  2319. continue;
  2320. }
  2321. }
  2322. }
  2323. gb_internal bool lb_llvm_object_generation(lbGenerator *gen, bool do_threading) {
  2324. LLVMCodeGenFileType code_gen_file_type = LLVMObjectFile;
  2325. if (build_context.build_mode == BuildMode_Assembly) {
  2326. code_gen_file_type = LLVMAssemblyFile;
  2327. }
  2328. char *llvm_error = nullptr;
  2329. defer (LLVMDisposeMessage(llvm_error));
  2330. if (do_threading) {
  2331. for (auto const &entry : gen->modules) {
  2332. lbModule *m = entry.value;
  2333. if (lb_is_module_empty(m)) {
  2334. continue;
  2335. }
  2336. String filepath_ll = lb_filepath_ll_for_module(m);
  2337. String filepath_obj = lb_filepath_obj_for_module(m);
  2338. array_add(&gen->output_object_paths, filepath_obj);
  2339. array_add(&gen->output_temp_paths, filepath_ll);
  2340. auto *wd = gb_alloc_item(permanent_allocator(), lbLLVMEmitWorker);
  2341. wd->target_machine = m->target_machine;
  2342. wd->code_gen_file_type = code_gen_file_type;
  2343. wd->filepath_obj = filepath_obj;
  2344. wd->m = m;
  2345. thread_pool_add_task(lb_llvm_emit_worker_proc, wd);
  2346. }
  2347. thread_pool_wait(&global_thread_pool);
  2348. } else {
  2349. for (auto const &entry : gen->modules) {
  2350. lbModule *m = entry.value;
  2351. if (lb_is_module_empty(m)) {
  2352. continue;
  2353. }
  2354. String filepath_obj = lb_filepath_obj_for_module(m);
  2355. array_add(&gen->output_object_paths, filepath_obj);
  2356. String short_name = remove_directory_from_path(filepath_obj);
  2357. gbString section_name = gb_string_make(permanent_allocator(), "LLVM Generate Object: ");
  2358. section_name = gb_string_append_length(section_name, short_name.text, short_name.len);
  2359. TIME_SECTION_WITH_LEN(section_name, gb_string_length(section_name));
  2360. if (LLVMTargetMachineEmitToFile(m->target_machine, m->mod, cast(char *)filepath_obj.text, code_gen_file_type, &llvm_error)) {
  2361. gb_printf_err("LLVM Error: %s\n", llvm_error);
  2362. exit_with_errors();
  2363. return false;
  2364. }
  2365. debugf("Generated File: %.*s\n", LIT(filepath_obj));
  2366. }
  2367. }
  2368. return true;
  2369. }
  2370. gb_internal lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *startup_runtime, lbProcedure *cleanup_runtime) {
  2371. LLVMPassManagerRef default_function_pass_manager = LLVMCreateFunctionPassManagerForModule(m->mod);
  2372. lb_populate_function_pass_manager(m, default_function_pass_manager, false, build_context.optimization_level);
  2373. LLVMFinalizeFunctionPassManager(default_function_pass_manager);
  2374. Type *params = alloc_type_tuple();
  2375. Type *results = alloc_type_tuple();
  2376. Type *t_ptr_cstring = alloc_type_pointer(t_cstring);
  2377. bool call_cleanup = true;
  2378. bool has_args = false;
  2379. bool is_dll_main = false;
  2380. String name = str_lit("main");
  2381. if (build_context.metrics.os == TargetOs_windows && build_context.build_mode == BuildMode_DynamicLibrary) {
  2382. is_dll_main = true;
  2383. name = str_lit("DllMain");
  2384. slice_init(&params->Tuple.variables, permanent_allocator(), 3);
  2385. params->Tuple.variables[0] = alloc_entity_param(nullptr, make_token_ident("hinstDLL"), t_rawptr, false, true);
  2386. params->Tuple.variables[1] = alloc_entity_param(nullptr, make_token_ident("fdwReason"), t_u32, false, true);
  2387. params->Tuple.variables[2] = alloc_entity_param(nullptr, make_token_ident("lpReserved"), t_rawptr, false, true);
  2388. call_cleanup = false;
  2389. } else if (build_context.metrics.os == TargetOs_windows && (build_context.metrics.arch == TargetArch_i386 || build_context.no_crt)) {
  2390. name = str_lit("mainCRTStartup");
  2391. } else if (is_arch_wasm()) {
  2392. name = str_lit("_start");
  2393. call_cleanup = false;
  2394. } else {
  2395. has_args = true;
  2396. slice_init(&params->Tuple.variables, permanent_allocator(), 2);
  2397. params->Tuple.variables[0] = alloc_entity_param(nullptr, make_token_ident("argc"), t_i32, false, true);
  2398. params->Tuple.variables[1] = alloc_entity_param(nullptr, make_token_ident("argv"), t_ptr_cstring, false, true);
  2399. }
  2400. slice_init(&results->Tuple.variables, permanent_allocator(), 1);
  2401. results->Tuple.variables[0] = alloc_entity_param(nullptr, blank_token, t_i32, false, true);
  2402. Type *proc_type = alloc_type_proc(nullptr,
  2403. params, params->Tuple.variables.count,
  2404. results, results->Tuple.variables.count, false, ProcCC_CDecl);
  2405. lbProcedure *p = lb_create_dummy_procedure(m, name, proc_type);
  2406. p->is_startup = true;
  2407. lb_begin_procedure_body(p);
  2408. if (has_args) { // initialize `runtime.args__`
  2409. lbValue argc = {LLVMGetParam(p->value, 0), t_i32};
  2410. lbValue argv = {LLVMGetParam(p->value, 1), t_ptr_cstring};
  2411. LLVMSetValueName2(argc.value, "argc", 4);
  2412. LLVMSetValueName2(argv.value, "argv", 4);
  2413. argc = lb_emit_conv(p, argc, t_int);
  2414. lbAddr args = lb_addr(lb_find_runtime_value(p->module, str_lit("args__")));
  2415. lb_fill_slice(p, args, argv, argc);
  2416. }
  2417. lbValue startup_runtime_value = {startup_runtime->value, startup_runtime->type};
  2418. lb_emit_call(p, startup_runtime_value, {}, ProcInlining_none);
  2419. if (build_context.command_kind == Command_test) {
  2420. Type *t_Internal_Test = find_type_in_pkg(m->info, str_lit("testing"), str_lit("Internal_Test"));
  2421. Type *array_type = alloc_type_array(t_Internal_Test, m->info->testing_procedures.count);
  2422. Type *slice_type = alloc_type_slice(t_Internal_Test);
  2423. lbAddr all_tests_array_addr = lb_add_global_generated_with_name(p->module, array_type, {}, str_lit("__$all_tests_array"));
  2424. lbValue all_tests_array = lb_addr_get_ptr(p, all_tests_array_addr);
  2425. LLVMValueRef indices[2] = {};
  2426. indices[0] = LLVMConstInt(lb_type(m, t_i32), 0, false);
  2427. isize testing_proc_index = 0;
  2428. for (Entity *testing_proc : m->info->testing_procedures) {
  2429. String name = testing_proc->token.string;
  2430. String pkg_name = {};
  2431. if (testing_proc->pkg != nullptr) {
  2432. pkg_name = testing_proc->pkg->name;
  2433. }
  2434. lbValue v_pkg = lb_find_or_add_entity_string(m, pkg_name);
  2435. lbValue v_name = lb_find_or_add_entity_string(m, name);
  2436. lbValue v_proc = lb_find_procedure_value_from_entity(m, testing_proc);
  2437. indices[1] = LLVMConstInt(lb_type(m, t_int), testing_proc_index++, false);
  2438. LLVMValueRef vals[3] = {};
  2439. vals[0] = v_pkg.value;
  2440. vals[1] = v_name.value;
  2441. vals[2] = v_proc.value;
  2442. GB_ASSERT(LLVMIsConstant(vals[0]));
  2443. GB_ASSERT(LLVMIsConstant(vals[1]));
  2444. GB_ASSERT(LLVMIsConstant(vals[2]));
  2445. LLVMValueRef dst = LLVMConstInBoundsGEP2(llvm_addr_type(m, all_tests_array), all_tests_array.value, indices, gb_count_of(indices));
  2446. LLVMValueRef src = llvm_const_named_struct(m, t_Internal_Test, vals, gb_count_of(vals));
  2447. LLVMBuildStore(p->builder, src, dst);
  2448. }
  2449. lbAddr all_tests_slice = lb_add_local_generated(p, slice_type, true);
  2450. lb_fill_slice(p, all_tests_slice,
  2451. lb_array_elem(p, all_tests_array),
  2452. lb_const_int(m, t_int, m->info->testing_procedures.count));
  2453. lbValue runner = lb_find_package_value(m, str_lit("testing"), str_lit("runner"));
  2454. TEMPORARY_ALLOCATOR_GUARD();
  2455. auto args = array_make<lbValue>(temporary_allocator(), 1);
  2456. args[0] = lb_addr_load(p, all_tests_slice);
  2457. lbValue result = lb_emit_call(p, runner, args);
  2458. lbValue exit_runner = lb_find_package_value(m, str_lit("os"), str_lit("exit"));
  2459. auto exit_args = array_make<lbValue>(temporary_allocator(), 1);
  2460. exit_args[0] = lb_emit_select(p, result, lb_const_int(m, t_int, 0), lb_const_int(m, t_int, 1));
  2461. lb_emit_call(p, exit_runner, exit_args, ProcInlining_none);
  2462. } else {
  2463. if (m->info->entry_point != nullptr) {
  2464. lbValue entry_point = lb_find_procedure_value_from_entity(m, m->info->entry_point);
  2465. lb_emit_call(p, entry_point, {}, ProcInlining_no_inline);
  2466. }
  2467. if (call_cleanup) {
  2468. lbValue cleanup_runtime_value = {cleanup_runtime->value, cleanup_runtime->type};
  2469. lb_emit_call(p, cleanup_runtime_value, {}, ProcInlining_none);
  2470. }
  2471. if (is_dll_main) {
  2472. LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_i32), 1, false));
  2473. } else {
  2474. LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_i32), 0, false));
  2475. }
  2476. }
  2477. lb_end_procedure_body(p);
  2478. LLVMSetLinkage(p->value, LLVMExternalLinkage);
  2479. if (is_arch_wasm()) {
  2480. lb_set_wasm_export_attributes(p->value, p->name);
  2481. }
  2482. lb_verify_function(m, p);
  2483. lb_run_function_pass_manager(default_function_pass_manager, p, lbFunctionPassManager_default);
  2484. return p;
  2485. }
  2486. gb_internal void lb_generate_procedure(lbModule *m, lbProcedure *p) {
  2487. if (p->is_done) {
  2488. return;
  2489. }
  2490. if (p->body != nullptr) { // Build Procedure
  2491. m->curr_procedure = p;
  2492. lb_begin_procedure_body(p);
  2493. lb_build_stmt(p, p->body);
  2494. lb_end_procedure_body(p);
  2495. p->is_done = true;
  2496. m->curr_procedure = nullptr;
  2497. }
  2498. lb_end_procedure(p);
  2499. // Add Flags
  2500. if (p->entity && p->entity->kind == Entity_Procedure && p->entity->Procedure.is_memcpy_like) {
  2501. p->flags |= lbProcedureFlag_WithoutMemcpyPass;
  2502. }
  2503. lb_verify_function(m, p, true);
  2504. }
  2505. gb_internal bool lb_generate_code(lbGenerator *gen) {
  2506. TIME_SECTION("LLVM Initializtion");
  2507. isize thread_count = gb_max(build_context.thread_count, 1);
  2508. isize worker_count = thread_count-1;
  2509. bool do_threading = !!(LLVMIsMultithreaded() && USE_SEPARATE_MODULES && MULTITHREAD_OBJECT_GENERATION && worker_count > 0);
  2510. lbModule *default_module = &gen->default_module;
  2511. CheckerInfo *info = gen->info;
  2512. auto *min_dep_set = &info->minimum_dependency_set;
  2513. switch (build_context.metrics.arch) {
  2514. case TargetArch_amd64:
  2515. case TargetArch_i386:
  2516. LLVMInitializeX86TargetInfo();
  2517. LLVMInitializeX86Target();
  2518. LLVMInitializeX86TargetMC();
  2519. LLVMInitializeX86AsmPrinter();
  2520. LLVMInitializeX86AsmParser();
  2521. LLVMInitializeX86Disassembler();
  2522. break;
  2523. case TargetArch_arm64:
  2524. LLVMInitializeAArch64TargetInfo();
  2525. LLVMInitializeAArch64Target();
  2526. LLVMInitializeAArch64TargetMC();
  2527. LLVMInitializeAArch64AsmPrinter();
  2528. LLVMInitializeAArch64AsmParser();
  2529. LLVMInitializeAArch64Disassembler();
  2530. break;
  2531. case TargetArch_wasm32:
  2532. case TargetArch_wasm64p32:
  2533. LLVMInitializeWebAssemblyTargetInfo();
  2534. LLVMInitializeWebAssemblyTarget();
  2535. LLVMInitializeWebAssemblyTargetMC();
  2536. LLVMInitializeWebAssemblyAsmPrinter();
  2537. LLVMInitializeWebAssemblyAsmParser();
  2538. LLVMInitializeWebAssemblyDisassembler();
  2539. break;
  2540. default:
  2541. LLVMInitializeAllTargetInfos();
  2542. LLVMInitializeAllTargets();
  2543. LLVMInitializeAllTargetMCs();
  2544. LLVMInitializeAllAsmPrinters();
  2545. LLVMInitializeAllAsmParsers();
  2546. LLVMInitializeAllDisassemblers();
  2547. break;
  2548. }
  2549. if (build_context.microarch == "native") {
  2550. LLVMInitializeNativeTarget();
  2551. }
  2552. char const *target_triple = alloc_cstring(permanent_allocator(), build_context.metrics.target_triplet);
  2553. for (auto const &entry : gen->modules) {
  2554. LLVMSetTarget(entry.value->mod, target_triple);
  2555. }
  2556. LLVMTargetRef target = {};
  2557. char *llvm_error = nullptr;
  2558. LLVMGetTargetFromTriple(target_triple, &target, &llvm_error);
  2559. GB_ASSERT(target != nullptr);
  2560. TIME_SECTION("LLVM Create Target Machine");
  2561. LLVMCodeModel code_mode = LLVMCodeModelDefault;
  2562. if (is_arch_wasm()) {
  2563. code_mode = LLVMCodeModelJITDefault;
  2564. } else if (is_arch_x86() && build_context.metrics.os == TargetOs_freestanding) {
  2565. code_mode = LLVMCodeModelKernel;
  2566. }
  2567. String llvm_cpu = get_final_microarchitecture();
  2568. gbString llvm_features = gb_string_make(temporary_allocator(), "");
  2569. String_Iterator it = {build_context.target_features_string, 0};
  2570. bool first = true;
  2571. for (;;) {
  2572. String str = string_split_iterator(&it, ',');
  2573. if (str == "") break;
  2574. if (!first) {
  2575. llvm_features = gb_string_appendc(llvm_features, ",");
  2576. }
  2577. first = false;
  2578. llvm_features = gb_string_appendc(llvm_features, "+");
  2579. llvm_features = gb_string_append_length(llvm_features, str.text, str.len);
  2580. }
  2581. debugf("CPU: %.*s, Features: %s\n", LIT(llvm_cpu), llvm_features);
  2582. // GB_ASSERT_MSG(LLVMTargetHasAsmBackend(target));
  2583. LLVMCodeGenOptLevel code_gen_level = LLVMCodeGenLevelNone;
  2584. if (!LB_USE_NEW_PASS_SYSTEM) {
  2585. build_context.optimization_level = gb_clamp(build_context.optimization_level, -1, 2);
  2586. }
  2587. switch (build_context.optimization_level) {
  2588. default:/*fallthrough*/
  2589. case 0: code_gen_level = LLVMCodeGenLevelNone; break;
  2590. case 1: code_gen_level = LLVMCodeGenLevelLess; break;
  2591. case 2: code_gen_level = LLVMCodeGenLevelDefault; break;
  2592. case 3: code_gen_level = LLVMCodeGenLevelAggressive; break;
  2593. }
  2594. // NOTE(bill): Target Machine Creation
  2595. // NOTE(bill, 2021-05-04): Target machines must be unique to each module because they are not thread safe
  2596. auto target_machines = array_make<LLVMTargetMachineRef>(permanent_allocator(), 0, gen->modules.count);
  2597. // NOTE(dweiler): Dynamic libraries require position-independent code.
  2598. LLVMRelocMode reloc_mode = LLVMRelocDefault;
  2599. if (build_context.build_mode == BuildMode_DynamicLibrary) {
  2600. reloc_mode = LLVMRelocPIC;
  2601. }
  2602. switch (build_context.reloc_mode) {
  2603. case RelocMode_Default:
  2604. if (build_context.metrics.os == TargetOs_openbsd || build_context.metrics.os == TargetOs_haiku) {
  2605. // Always use PIC for OpenBSD and Haiku: they default to PIE
  2606. reloc_mode = LLVMRelocPIC;
  2607. }
  2608. if (build_context.metrics.arch == TargetArch_riscv64) {
  2609. // NOTE(laytan): didn't seem to work without this.
  2610. reloc_mode = LLVMRelocPIC;
  2611. }
  2612. break;
  2613. case RelocMode_Static:
  2614. reloc_mode = LLVMRelocStatic;
  2615. break;
  2616. case RelocMode_PIC:
  2617. reloc_mode = LLVMRelocPIC;
  2618. break;
  2619. case RelocMode_DynamicNoPIC:
  2620. reloc_mode = LLVMRelocDynamicNoPic;
  2621. break;
  2622. }
  2623. for (auto const &entry : gen->modules) {
  2624. LLVMTargetMachineRef target_machine = LLVMCreateTargetMachine(
  2625. target, target_triple, (const char *)llvm_cpu.text,
  2626. llvm_features,
  2627. code_gen_level,
  2628. reloc_mode,
  2629. code_mode);
  2630. lbModule *m = entry.value;
  2631. m->target_machine = target_machine;
  2632. LLVMSetModuleDataLayout(m->mod, LLVMCreateTargetDataLayout(target_machine));
  2633. #if LLVM_VERSION_MAJOR >= 18
  2634. if (build_context.fast_isel) {
  2635. LLVMSetTargetMachineFastISel(m->target_machine, true);
  2636. }
  2637. #endif
  2638. array_add(&target_machines, target_machine);
  2639. }
  2640. for (auto const &entry : gen->modules) {
  2641. lbModule *m = entry.value;
  2642. if (m->debug_builder) { // Debug Info
  2643. for (auto const &file_entry : info->files) {
  2644. AstFile *f = file_entry.value;
  2645. LLVMMetadataRef res = LLVMDIBuilderCreateFile(m->debug_builder,
  2646. cast(char const *)f->filename.text, f->filename.len,
  2647. cast(char const *)f->directory.text, f->directory.len);
  2648. lb_set_llvm_metadata(m, f, res);
  2649. }
  2650. TEMPORARY_ALLOCATOR_GUARD();
  2651. gbString producer = gb_string_make(temporary_allocator(), "odin");
  2652. // producer = gb_string_append_fmt(producer, " version %.*s", LIT(ODIN_VERSION));
  2653. // #ifdef NIGHTLY
  2654. // producer = gb_string_appendc(producer, "-nightly");
  2655. // #endif
  2656. // #ifdef GIT_SHA
  2657. // producer = gb_string_append_fmt(producer, "-%s", GIT_SHA);
  2658. // #endif
  2659. gbString split_name = gb_string_make(temporary_allocator(), "");
  2660. LLVMBool is_optimized = build_context.optimization_level > 0;
  2661. AstFile *init_file = m->info->init_package->files[0];
  2662. if (Entity *entry_point = m->info->entry_point) {
  2663. if (Ast *ident = entry_point->identifier.load()) {
  2664. if (ident->file_id) {
  2665. init_file = ident->file();
  2666. }
  2667. }
  2668. }
  2669. LLVMBool split_debug_inlining = build_context.build_mode == BuildMode_Assembly;
  2670. LLVMBool debug_info_for_profiling = false;
  2671. m->debug_compile_unit = LLVMDIBuilderCreateCompileUnit(m->debug_builder, LLVMDWARFSourceLanguageC99,
  2672. lb_get_llvm_metadata(m, init_file),
  2673. producer, gb_string_length(producer),
  2674. is_optimized, "", 0,
  2675. 1, split_name, gb_string_length(split_name),
  2676. LLVMDWARFEmissionFull,
  2677. 0, split_debug_inlining,
  2678. debug_info_for_profiling,
  2679. "", 0, // sys_root
  2680. "", 0 // SDK
  2681. );
  2682. GB_ASSERT(m->debug_compile_unit != nullptr);
  2683. }
  2684. }
  2685. TIME_SECTION("LLVM Global Variables");
  2686. if (!build_context.no_rtti) {
  2687. lbModule *m = default_module;
  2688. { // Add type info data
  2689. // GB_ASSERT_MSG(info->minimum_dependency_type_info_index_map.count == info->type_info_types.count, "%tu vs %tu", info->minimum_dependency_type_info_index_map.count, info->type_info_types.count);
  2690. // isize max_type_info_count = info->minimum_dependency_type_info_index_map.count+1;
  2691. isize max_type_info_count = info->type_info_types_hash_map.count;
  2692. Type *t = alloc_type_array(t_type_info_ptr, max_type_info_count);
  2693. // IMPORTANT NOTE(bill): As LLVM does not have a union type, an array of unions cannot be initialized
  2694. // at compile time without cheating in some way. This means to emulate an array of unions is to use
  2695. // a giant packed struct of "corrected" data types.
  2696. LLVMTypeRef internal_llvm_type = lb_type(m, t);
  2697. LLVMValueRef g = LLVMAddGlobal(m->mod, internal_llvm_type, LB_TYPE_INFO_DATA_NAME);
  2698. LLVMSetInitializer(g, LLVMConstNull(internal_llvm_type));
  2699. LLVMSetLinkage(g, USE_SEPARATE_MODULES ? LLVMExternalLinkage : LLVMInternalLinkage);
  2700. LLVMSetUnnamedAddress(g, LLVMGlobalUnnamedAddr);
  2701. LLVMSetGlobalConstant(g, true);
  2702. lbValue value = {};
  2703. value.value = g;
  2704. value.type = alloc_type_pointer(t);
  2705. lb_global_type_info_data_entity = alloc_entity_variable(nullptr, make_token_ident(LB_TYPE_INFO_DATA_NAME), t, EntityState_Resolved);
  2706. lb_add_entity(m, lb_global_type_info_data_entity, value);
  2707. }
  2708. { // Type info member buffer
  2709. // NOTE(bill): Removes need for heap allocation by making it global memory
  2710. isize count = 0;
  2711. isize offsets_extra = 0;
  2712. for (auto const &tt : m->info->type_info_types_hash_map) {
  2713. Type *t = tt.type;
  2714. if (t == nullptr) {
  2715. continue;
  2716. }
  2717. isize index = lb_type_info_index(m->info, t, false);
  2718. if (index < 0) {
  2719. continue;
  2720. }
  2721. switch (t->kind) {
  2722. case Type_Union:
  2723. count += t->Union.variants.count;
  2724. break;
  2725. case Type_Struct:
  2726. count += t->Struct.fields.count;
  2727. break;
  2728. case Type_Tuple:
  2729. count += t->Tuple.variables.count;
  2730. break;
  2731. case Type_BitField:
  2732. count += t->BitField.fields.count;
  2733. // Twice is needed for the bit_offsets
  2734. offsets_extra += t->BitField.fields.count;
  2735. break;
  2736. }
  2737. }
  2738. auto const global_type_info_make = [](lbModule *m, char const *name, Type *elem_type, i64 count) -> lbAddr {
  2739. Type *t = alloc_type_array(elem_type, count);
  2740. LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), name);
  2741. LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t)));
  2742. LLVMSetLinkage(g, LLVMInternalLinkage);
  2743. lb_make_global_private_const(g);
  2744. return lb_addr({g, alloc_type_pointer(t)});
  2745. };
  2746. lb_global_type_info_member_types = global_type_info_make(m, LB_TYPE_INFO_TYPES_NAME, t_type_info_ptr, count);
  2747. lb_global_type_info_member_names = global_type_info_make(m, LB_TYPE_INFO_NAMES_NAME, t_string, count);
  2748. lb_global_type_info_member_offsets = global_type_info_make(m, LB_TYPE_INFO_OFFSETS_NAME, t_uintptr, count+offsets_extra);
  2749. lb_global_type_info_member_usings = global_type_info_make(m, LB_TYPE_INFO_USINGS_NAME, t_bool, count);
  2750. lb_global_type_info_member_tags = global_type_info_make(m, LB_TYPE_INFO_TAGS_NAME, t_string, count);
  2751. }
  2752. }
  2753. isize global_variable_max_count = 0;
  2754. bool already_has_entry_point = false;
  2755. for (Entity *e : info->entities) {
  2756. String name = e->token.string;
  2757. if (e->kind == Entity_Variable) {
  2758. global_variable_max_count++;
  2759. } else if (e->kind == Entity_Procedure) {
  2760. if ((e->scope->flags&ScopeFlag_Init) && name == "main") {
  2761. GB_ASSERT(e == info->entry_point);
  2762. }
  2763. if (build_context.command_kind == Command_test &&
  2764. (e->Procedure.is_export || e->Procedure.link_name.len > 0)) {
  2765. String link_name = e->Procedure.link_name;
  2766. if (e->pkg->kind == Package_Runtime) {
  2767. if (link_name == "main" ||
  2768. link_name == "DllMain" ||
  2769. link_name == "WinMain" ||
  2770. link_name == "wWinMain" ||
  2771. link_name == "mainCRTStartup" ||
  2772. link_name == "_start") {
  2773. already_has_entry_point = true;
  2774. }
  2775. }
  2776. }
  2777. }
  2778. }
  2779. auto global_variables = array_make<lbGlobalVariable>(permanent_allocator(), 0, global_variable_max_count);
  2780. for (DeclInfo *d : info->variable_init_order) {
  2781. Entity *e = d->entity;
  2782. if ((e->scope->flags & ScopeFlag_File) == 0) {
  2783. continue;
  2784. }
  2785. if (!ptr_set_exists(min_dep_set, e)) {
  2786. continue;
  2787. }
  2788. DeclInfo *decl = decl_info_of_entity(e);
  2789. if (decl == nullptr) {
  2790. continue;
  2791. }
  2792. GB_ASSERT(e->kind == Entity_Variable);
  2793. bool is_foreign = e->Variable.is_foreign;
  2794. bool is_export = e->Variable.is_export;
  2795. lbModule *m = &gen->default_module;
  2796. String name = lb_get_entity_name(m, e);
  2797. lbValue g = {};
  2798. g.value = LLVMAddGlobal(m->mod, lb_type(m, e->type), alloc_cstring(permanent_allocator(), name));
  2799. g.type = alloc_type_pointer(e->type);
  2800. if (e->Variable.thread_local_model != "") {
  2801. LLVMSetThreadLocal(g.value, true);
  2802. String m = e->Variable.thread_local_model;
  2803. LLVMThreadLocalMode mode = LLVMGeneralDynamicTLSModel;
  2804. if (m == "default") {
  2805. mode = LLVMGeneralDynamicTLSModel;
  2806. } else if (m == "localdynamic") {
  2807. mode = LLVMLocalDynamicTLSModel;
  2808. } else if (m == "initialexec") {
  2809. mode = LLVMInitialExecTLSModel;
  2810. } else if (m == "localexec") {
  2811. mode = LLVMLocalExecTLSModel;
  2812. } else {
  2813. GB_PANIC("Unhandled thread local mode %.*s", LIT(m));
  2814. }
  2815. LLVMSetThreadLocalMode(g.value, mode);
  2816. }
  2817. if (is_foreign) {
  2818. LLVMSetLinkage(g.value, LLVMExternalLinkage);
  2819. LLVMSetDLLStorageClass(g.value, LLVMDLLImportStorageClass);
  2820. LLVMSetExternallyInitialized(g.value, true);
  2821. lb_add_foreign_library_path(m, e->Variable.foreign_library);
  2822. } else {
  2823. LLVMSetInitializer(g.value, LLVMConstNull(lb_type(m, e->type)));
  2824. }
  2825. if (is_export) {
  2826. LLVMSetLinkage(g.value, LLVMDLLExportLinkage);
  2827. LLVMSetDLLStorageClass(g.value, LLVMDLLExportStorageClass);
  2828. } else if (!is_foreign) {
  2829. LLVMSetLinkage(g.value, USE_SEPARATE_MODULES ? LLVMWeakAnyLinkage : LLVMInternalLinkage);
  2830. }
  2831. lb_set_linkage_from_entity_flags(m, g.value, e->flags);
  2832. if (e->Variable.link_section.len > 0) {
  2833. LLVMSetSection(g.value, alloc_cstring(permanent_allocator(), e->Variable.link_section));
  2834. }
  2835. lbGlobalVariable var = {};
  2836. var.var = g;
  2837. var.decl = decl;
  2838. if (decl->init_expr != nullptr) {
  2839. TypeAndValue tav = type_and_value_of_expr(decl->init_expr);
  2840. if (!is_type_any(e->type) && !is_type_union(e->type)) {
  2841. if (tav.mode != Addressing_Invalid) {
  2842. if (tav.value.kind != ExactValue_Invalid) {
  2843. bool is_rodata = e->kind == Entity_Variable && e->Variable.is_rodata;
  2844. ExactValue v = tav.value;
  2845. lbValue init = lb_const_value(m, tav.type, v, false, is_rodata);
  2846. LLVMSetInitializer(g.value, init.value);
  2847. var.is_initialized = true;
  2848. if (is_rodata) {
  2849. LLVMSetGlobalConstant(g.value, true);
  2850. }
  2851. }
  2852. }
  2853. }
  2854. if (!var.is_initialized && is_type_untyped_nil(tav.type)) {
  2855. var.is_initialized = true;
  2856. if (e->kind == Entity_Variable && e->Variable.is_rodata) {
  2857. LLVMSetGlobalConstant(g.value, true);
  2858. }
  2859. }
  2860. } else if (e->kind == Entity_Variable && e->Variable.is_rodata) {
  2861. LLVMSetGlobalConstant(g.value, true);
  2862. }
  2863. array_add(&global_variables, var);
  2864. lb_add_entity(m, e, g);
  2865. lb_add_member(m, name, g);
  2866. if (m->debug_builder) {
  2867. String global_name = e->token.string;
  2868. if (global_name.len != 0 && global_name != "_") {
  2869. LLVMMetadataRef llvm_file = lb_get_llvm_metadata(m, e->file);
  2870. LLVMMetadataRef llvm_scope = llvm_file;
  2871. LLVMBool local_to_unit = LLVMGetLinkage(g.value) == LLVMInternalLinkage;
  2872. LLVMMetadataRef llvm_expr = LLVMDIBuilderCreateExpression(m->debug_builder, nullptr, 0);
  2873. LLVMMetadataRef llvm_decl = nullptr;
  2874. u32 align_in_bits = cast(u32)(8*type_align_of(e->type));
  2875. LLVMMetadataRef global_variable_metadata = LLVMDIBuilderCreateGlobalVariableExpression(
  2876. m->debug_builder, llvm_scope,
  2877. cast(char const *)global_name.text, global_name.len,
  2878. "", 0, // linkage
  2879. llvm_file, e->token.pos.line,
  2880. lb_debug_type(m, e->type),
  2881. local_to_unit,
  2882. llvm_expr,
  2883. llvm_decl,
  2884. align_in_bits
  2885. );
  2886. lb_set_llvm_metadata(m, g.value, global_variable_metadata);
  2887. LLVMGlobalSetMetadata(g.value, 0, global_variable_metadata);
  2888. }
  2889. }
  2890. }
  2891. TIME_SECTION("LLVM Runtime Objective-C Names Creation");
  2892. gen->objc_names = lb_create_objc_names(default_module);
  2893. TIME_SECTION("LLVM Runtime Startup Creation (Global Variables & @(init))");
  2894. gen->startup_runtime = lb_create_startup_runtime(default_module, gen->objc_names, global_variables);
  2895. TIME_SECTION("LLVM Runtime Cleanup Creation & @(fini)");
  2896. gen->cleanup_runtime = lb_create_cleanup_runtime(default_module);
  2897. if (build_context.ODIN_DEBUG) {
  2898. for (auto const &entry : builtin_pkg->scope->elements) {
  2899. Entity *e = entry.value;
  2900. add_debug_info_for_global_constant_from_entity(gen, e);
  2901. }
  2902. }
  2903. if (gen->modules.count <= 1) {
  2904. do_threading = false;
  2905. }
  2906. TIME_SECTION("LLVM Global Procedures and Types");
  2907. lb_create_global_procedures_and_types(gen, info, do_threading);
  2908. TIME_SECTION("LLVM Procedure Generation");
  2909. lb_generate_procedures(gen, do_threading);
  2910. if (build_context.command_kind == Command_test && !already_has_entry_point) {
  2911. TIME_SECTION("LLVM main");
  2912. lb_create_main_procedure(default_module, gen->startup_runtime, gen->cleanup_runtime);
  2913. }
  2914. TIME_SECTION("LLVM Procedure Generation (missing)");
  2915. lb_generate_missing_procedures(gen, do_threading);
  2916. if (gen->objc_names) {
  2917. TIME_SECTION("Finalize objc names");
  2918. lb_finalize_objc_names(gen->objc_names);
  2919. }
  2920. if (build_context.ODIN_DEBUG) {
  2921. TIME_SECTION("LLVM Debug Info Complete Types and Finalize");
  2922. lb_debug_info_complete_types_and_finalize(gen);
  2923. }
  2924. if (do_threading) {
  2925. isize non_empty_module_count = 0;
  2926. for (auto const &entry : gen->modules) {
  2927. lbModule *m = entry.value;
  2928. if (!lb_is_module_empty(m)) {
  2929. non_empty_module_count += 1;
  2930. }
  2931. }
  2932. if (non_empty_module_count <= 1) {
  2933. do_threading = false;
  2934. }
  2935. }
  2936. TIME_SECTION("LLVM Function Pass");
  2937. lb_llvm_function_passes(gen, do_threading && !build_context.ODIN_DEBUG);
  2938. TIME_SECTION("LLVM Module Pass");
  2939. lb_llvm_module_passes(gen, do_threading);
  2940. TIME_SECTION("LLVM Module Verification");
  2941. if (!lb_llvm_module_verification(gen, do_threading)) {
  2942. return false;
  2943. }
  2944. llvm_error = nullptr;
  2945. defer (LLVMDisposeMessage(llvm_error));
  2946. if (build_context.keep_temp_files ||
  2947. build_context.build_mode == BuildMode_LLVM_IR) {
  2948. TIME_SECTION("LLVM Print Module to File");
  2949. for (auto const &entry : gen->modules) {
  2950. lbModule *m = entry.value;
  2951. if (lb_is_module_empty(m)) {
  2952. continue;
  2953. }
  2954. String filepath_ll = lb_filepath_ll_for_module(m);
  2955. if (LLVMPrintModuleToFile(m->mod, cast(char const *)filepath_ll.text, &llvm_error)) {
  2956. gb_printf_err("LLVM Error: %s\n", llvm_error);
  2957. exit_with_errors();
  2958. return false;
  2959. }
  2960. array_add(&gen->output_temp_paths, filepath_ll);
  2961. }
  2962. if (build_context.build_mode == BuildMode_LLVM_IR) {
  2963. return true;
  2964. }
  2965. }
  2966. TIME_SECTION("LLVM Add Foreign Library Paths");
  2967. lb_add_foreign_library_paths(gen);
  2968. TIME_SECTION("LLVM Correct Entity Linkage");
  2969. lb_correct_entity_linkage(gen);
  2970. ////////////////////////////////////////////
  2971. for (auto const &entry: gen->modules) {
  2972. lbModule *m = entry.value;
  2973. if (!lb_is_module_empty(m)) {
  2974. gen->used_module_count += 1;
  2975. }
  2976. }
  2977. gbString label_object_generation = gb_string_make(heap_allocator(), "LLVM Object Generation");
  2978. if (gen->used_module_count > 1) {
  2979. label_object_generation = gb_string_append_fmt(label_object_generation, " (%td used modules)", gen->used_module_count);
  2980. }
  2981. TIME_SECTION_WITH_LEN(label_object_generation, gb_string_length(label_object_generation));
  2982. if (build_context.ignore_llvm_build) {
  2983. gb_printf_err("LLVM object generation has been ignored!\n");
  2984. return false;
  2985. }
  2986. if (!lb_llvm_object_generation(gen, do_threading)) {
  2987. return false;
  2988. }
  2989. if (build_context.sanitizer_flags & SanitizerFlag_Address) {
  2990. if (build_context.metrics.os == TargetOs_windows) {
  2991. auto paths = array_make<String>(heap_allocator(), 0, 1);
  2992. String path = concatenate_strings(permanent_allocator(), build_context.ODIN_ROOT, str_lit("\\bin\\llvm\\windows\\clang_rt.asan-x86_64.lib"));
  2993. array_add(&paths, path);
  2994. Entity *lib = alloc_entity_library_name(nullptr, make_token_ident("asan_lib"), nullptr, slice_from_array(paths), str_lit("asan_lib"));
  2995. array_add(&gen->foreign_libraries, lib);
  2996. } else if (build_context.metrics.os == TargetOs_darwin || build_context.metrics.os == TargetOs_linux) {
  2997. if (!build_context.extra_linker_flags.text) {
  2998. build_context.extra_linker_flags = str_lit("-fsanitize=address");
  2999. } else {
  3000. build_context.extra_linker_flags = concatenate_strings(permanent_allocator(), build_context.extra_linker_flags, str_lit(" -fsanitize=address"));
  3001. }
  3002. }
  3003. }
  3004. if (build_context.sanitizer_flags & SanitizerFlag_Memory) {
  3005. if (build_context.metrics.os == TargetOs_darwin || build_context.metrics.os == TargetOs_linux) {
  3006. if (!build_context.extra_linker_flags.text) {
  3007. build_context.extra_linker_flags = str_lit("-fsanitize=memory");
  3008. } else {
  3009. build_context.extra_linker_flags = concatenate_strings(permanent_allocator(), build_context.extra_linker_flags, str_lit(" -fsanitize=memory"));
  3010. }
  3011. }
  3012. }
  3013. if (build_context.sanitizer_flags & SanitizerFlag_Thread) {
  3014. if (build_context.metrics.os == TargetOs_darwin || build_context.metrics.os == TargetOs_linux) {
  3015. if (!build_context.extra_linker_flags.text) {
  3016. build_context.extra_linker_flags = str_lit("-fsanitize=thread");
  3017. } else {
  3018. build_context.extra_linker_flags = concatenate_strings(permanent_allocator(), build_context.extra_linker_flags, str_lit(" -fsanitize=thread"));
  3019. }
  3020. }
  3021. }
  3022. array_sort(gen->foreign_libraries, foreign_library_cmp);
  3023. return true;
  3024. }