llvm.org GIT mirror llvm / 9ef76b9
Update the OCaml Kaleidoscope tutorial. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@97965 91177308-0d34-0410-b5e6-96231b3b80d8 Erick Tryzelaar 9 years ago
5 changed file(s) with 58 addition(s) and 65 deletion(s). Raw diff Collapse all Expand all
9797 let the_module = create_module (global_context ()) "my cool jit"
9898 let builder = builder (global_context ())
9999 let named_values:(string, llvalue) Hashtbl.t = Hashtbl.create 10
100 let double_type = double_type context
100101
101102
102103
388389
389390

                  
                
390391 (* Create a new basic block to start insertion into. *)
391 let bb = append_block "entry" the_function in
392 let bb = append_block context "entry" the_function in
392393 position_at_end bb builder;
393394
394395 try
902903 let the_module = create_module context "my cool jit"
903904 let builder = builder context
904905 let named_values:(string, llvalue) Hashtbl.t = Hashtbl.create 10
906 let double_type = double_type context
905907
906908 let rec codegen_expr = function
907909 | Ast.Number n -> const_float double_type n
973975 let the_function = codegen_proto proto in
974976
975977 (* Create a new basic block to start insertion into. *)
976 let bb = append_block "entry" the_function in
978 let bb = append_block context "entry" the_function in
977979 position_at_end bb builder;
978980
979981 try
185185
186186

                  
                
187187 (* Create the JIT. *)
188 let the_module_provider = ModuleProvider.create Codegen.the_module in
189 let the_execution_engine = ExecutionEngine.create the_module_provider in
190 let the_fpm = PassManager.create_function the_module_provider in
188 let the_execution_engine = ExecutionEngine.create Codegen.the_module in
189 let the_fpm = PassManager.create_function Codegen.the_module in
191190
192191 (* Set up the optimizer pipeline. Start with registering info about how the
193192 * target lays out data structures. *)
212211
213212
214213
215

This code defines two values, an Llvm.llmoduleprovider and a

216 Llvm.PassManager.t. The former is basically a wrapper around our
217 Llvm.llmodule that the Llvm.PassManager.t requires. It
218 provides certain flexibility that we're not going to take advantage of here,
219 so I won't dive into any details about it.

220
221214

The meat of the matter here, is the definition of "the_fpm". It

222 requires a pointer to the the_module (through the
223 the_module_provider) to construct itself. Once it is set up, we use a
224 series of "add" calls to add a bunch of LLVM passes. The first pass is
225 basically boilerplate, it adds a pass so that later optimizations know how the
226 data structures in the program are laid out. The
215 requires a pointer to the the_module to construct itself. Once it is
216 set up, we use a series of "add" calls to add a bunch of LLVM passes. The
217 first pass is basically boilerplate, it adds a pass so that later optimizations
218 know how the data structures in the program are laid out. The
227219 "the_execution_engine" variable is related to the JIT, which we will
228220 get to in the next section.

229221
319311 let main () =
320312 ...
321313 (* Create the JIT. *)
322 let the_module_provider = ModuleProvider.create Codegen.the_module in
323 let the_execution_engine = ExecutionEngine.create the_module_provider in
314 let the_execution_engine = ExecutionEngine.create Codegen.the_module in
324315 ...
325316
326317
350341 the_execution_engine in
351342
352343 print_string "Evaluated to ";
353 print_float (GenericValue.as_float double_type result);
344 print_float (GenericValue.as_float Codegen.double_type result);
354345 print_newline ();
355346
356347
795786 let the_module = create_module context "my cool jit"
796787 let builder = builder context
797788 let named_values:(string, llvalue) Hashtbl.t = Hashtbl.create 10
789 let double_type = double_type context
798790
799791 let rec codegen_expr = function
800792 | Ast.Number n -> const_float double_type n
866858 let the_function = codegen_proto proto in
867859
868860 (* Create a new basic block to start insertion into. *)
869 let bb = append_block "entry" the_function in
861 let bb = append_block context "entry" the_function in
870862 position_at_end bb builder;
871863
872864 try
931923 the_execution_engine in
932924
933925 print_string "Evaluated to ";
934 print_float (GenericValue.as_float double_type result);
926 print_float (GenericValue.as_float Codegen.double_type result);
935927 print_newline ();
936928 with Stream.Error s | Codegen.Error s ->
937929 (* Skip token for error recovery. *)
970962 let stream = Lexer.lex (Stream.of_channel stdin) in
971963
972964 (* Create the JIT. *)
973 let the_module_provider = ModuleProvider.create Codegen.the_module in
974 let the_execution_engine = ExecutionEngine.create the_module_provider in
975 let the_fpm = PassManager.create_function the_module_provider in
965 let the_execution_engine = ExecutionEngine.create Codegen.the_module in
966 let the_fpm = PassManager.create_function Codegen.the_module in
976967
977968 (* Set up the optimizer pipeline. Start with registering info about how the
978969 * target lays out data structures. *)
979970 TargetData.add (ExecutionEngine.target_data the_execution_engine) the_fpm;
980971
981972 (* Do simple "peephole" optimizations and bit-twiddling optzn. *)
982 add_instruction_combining the_fpm;
973 add_instruction_combination the_fpm;
983974
984975 (* reassociate expressions. *)
985976 add_reassociation the_fpm;
363363 let start_bb = insertion_block builder in
364364 let the_function = block_parent start_bb in
365365
366 let then_bb = append_block "then" the_function in
366 let then_bb = append_block context "then" the_function in
367367 position_at_end then_bb builder;
368368
369369
416416
417417

                  
                
418418 (* Emit 'else' value. *)
419 let else_bb = append_block "else" the_function in
419 let else_bb = append_block context "else" the_function in
420420 position_at_end else_bb builder;
421421 let else_val = codegen_expr else_ in
422422
432432
433433

                  
                
434434 (* Emit merge block. *)
435 let merge_bb = append_block "ifcont" the_function in
435 let merge_bb = append_block context "ifcont" the_function in
436436 position_at_end merge_bb builder;
437437 let incoming = [(then_val, new_then_bb); (else_val, new_else_bb)] in
438438 let phi = build_phi incoming "iftmp" builder in
703703 * block. *)
704704 let preheader_bb = insertion_block builder in
705705 let the_function = block_parent preheader_bb in
706 let loop_bb = append_block "loop" the_function in
706 let loop_bb = append_block context "loop" the_function in
707707
708708 (* Insert an explicit fall through from the current block to the
709709 * loop_bb. *)
803803

                  
                
804804 (* Create the "after loop" block and insert it. *)
805805 let loop_end_bb = insertion_block builder in
806 let after_bb = append_block "afterloop" the_function in
806 let after_bb = append_block context "afterloop" the_function in
807807
808808 (* Insert the conditional branch into the end of loop_end_bb. *)
809809 ignore (build_cond_br end_cond loop_bb after_bb builder);
12031203 let the_module = create_module context "my cool jit"
12041204 let builder = builder context
12051205 let named_values:(string, llvalue) Hashtbl.t = Hashtbl.create 10
1206 let double_type = double_type context
12061207
12071208 let rec codegen_expr = function
12081209 | Ast.Number n -> const_float double_type n
12491250 let start_bb = insertion_block builder in
12501251 let the_function = block_parent start_bb in
12511252
1252 let then_bb = append_block "then" the_function in
1253 let then_bb = append_block context "then" the_function in
12531254
12541255 (* Emit 'then' value. *)
12551256 position_at_end then_bb builder;
12611262 let new_then_bb = insertion_block builder in
12621263
12631264 (* Emit 'else' value. *)
1264 let else_bb = append_block "else" the_function in
1265 let else_bb = append_block context "else" the_function in
12651266 position_at_end else_bb builder;
12661267 let else_val = codegen_expr else_ in
12671268
12701271 let new_else_bb = insertion_block builder in
12711272
12721273 (* Emit merge block. *)
1273 let merge_bb = append_block "ifcont" the_function in
1274 let merge_bb = append_block context "ifcont" the_function in
12741275 position_at_end merge_bb builder;
12751276 let incoming = [(then_val, new_then_bb); (else_val, new_else_bb)] in
12761277 let phi = build_phi incoming "iftmp" builder in
12961297 * block. *)
12971298 let preheader_bb = insertion_block builder in
12981299 let the_function = block_parent preheader_bb in
1299 let loop_bb = append_block "loop" the_function in
1300 let loop_bb = append_block context "loop" the_function in
13001301
13011302 (* Insert an explicit fall through from the current block to the
13021303 * loop_bb. *)
13401341
13411342 (* Create the "after loop" block and insert it. *)
13421343 let loop_end_bb = insertion_block builder in
1343 let after_bb = append_block "afterloop" the_function in
1344 let after_bb = append_block context "afterloop" the_function in
13441345
13451346 (* Insert the conditional branch into the end of loop_end_bb. *)
13461347 ignore (build_cond_br end_cond loop_bb after_bb builder);
13961397 let the_function = codegen_proto proto in
13971398
13981399 (* Create a new basic block to start insertion into. *)
1399 let bb = append_block "entry" the_function in
1400 let bb = append_block context "entry" the_function in
14001401 position_at_end bb builder;
14011402
14021403 try
14611462 the_execution_engine in
14621463
14631464 print_string "Evaluated to ";
1464 print_float (GenericValue.as_float double_type result);
1465 print_float (GenericValue.as_float Codegen.double_type result);
14651466 print_newline ();
14661467 with Stream.Error s | Codegen.Error s ->
14671468 (* Skip token for error recovery. *)
15001501 let stream = Lexer.lex (Stream.of_channel stdin) in
15011502
15021503 (* Create the JIT. *)
1503 let the_module_provider = ModuleProvider.create Codegen.the_module in
1504 let the_execution_engine = ExecutionEngine.create the_module_provider in
1505 let the_fpm = PassManager.create_function the_module_provider in
1504 let the_execution_engine = ExecutionEngine.create Codegen.the_module in
1505 let the_fpm = PassManager.create_function Codegen.the_module in
15061506
15071507 (* Set up the optimizer pipeline. Start with registering info about how the
15081508 * target lays out data structures. *)
15091509 TargetData.add (ExecutionEngine.target_data the_execution_engine) the_fpm;
15101510
15111511 (* Do simple "peephole" optimizations and bit-twiddling optzn. *)
1512 add_instruction_combining the_fpm;
1512 add_instruction_combination the_fpm;
15131513
15141514 (* reassociate expressions. *)
15151515 add_reassociation the_fpm;
299299 end;
300300
301301 (* Create a new basic block to start insertion into. *)
302 let bb = append_block "entry" the_function in
302 let bb = append_block context "entry" the_function in
303303 position_at_end bb builder;
304304 ...
305305
11761176 let the_module = create_module context "my cool jit"
11771177 let builder = builder context
11781178 let named_values:(string, llvalue) Hashtbl.t = Hashtbl.create 10
1179 let double_type = double_type context
11791180
11801181 let rec codegen_expr = function
11811182 | Ast.Number n -> const_float double_type n
12401241 let start_bb = insertion_block builder in
12411242 let the_function = block_parent start_bb in
12421243
1243 let then_bb = append_block "then" the_function in
1244 let then_bb = append_block context "then" the_function in
12441245
12451246 (* Emit 'then' value. *)
12461247 position_at_end then_bb builder;
12521253 let new_then_bb = insertion_block builder in
12531254
12541255 (* Emit 'else' value. *)
1255 let else_bb = append_block "else" the_function in
1256 let else_bb = append_block context "else" the_function in
12561257 position_at_end else_bb builder;
12571258 let else_val = codegen_expr else_ in
12581259
12611262 let new_else_bb = insertion_block builder in
12621263
12631264 (* Emit merge block. *)
1264 let merge_bb = append_block "ifcont" the_function in
1265 let merge_bb = append_block context "ifcont" the_function in
12651266 position_at_end merge_bb builder;
12661267 let incoming = [(then_val, new_then_bb); (else_val, new_else_bb)] in
12671268 let phi = build_phi incoming "iftmp" builder in
12871288 * block. *)
12881289 let preheader_bb = insertion_block builder in
12891290 let the_function = block_parent preheader_bb in
1290 let loop_bb = append_block "loop" the_function in
1291 let loop_bb = append_block context "loop" the_function in
12911292
12921293 (* Insert an explicit fall through from the current block to the
12931294 * loop_bb. *)
13311332
13321333 (* Create the "after loop" block and insert it. *)
13331334 let loop_end_bb = insertion_block builder in
1334 let after_bb = append_block "afterloop" the_function in
1335 let after_bb = append_block context "afterloop" the_function in
13351336
13361337 (* Insert the conditional branch into the end of loop_end_bb. *)
13371338 ignore (build_cond_br end_cond loop_bb after_bb builder);
13951396 end;
13961397
13971398 (* Create a new basic block to start insertion into. *)
1398 let bb = append_block "entry" the_function in
1399 let bb = append_block context "entry" the_function in
13991400 position_at_end bb builder;
14001401
14011402 try
14601461 the_execution_engine in
14611462
14621463 print_string "Evaluated to ";
1463 print_float (GenericValue.as_float double_type result);
1464 print_float (GenericValue.as_float Codegen.double_type result);
14641465 print_newline ();
14651466 with Stream.Error s | Codegen.Error s ->
14661467 (* Skip token for error recovery. *)
14991500 let stream = Lexer.lex (Stream.of_channel stdin) in
15001501
15011502 (* Create the JIT. *)
1502 let the_module_provider = ModuleProvider.create Codegen.the_module in
1503 let the_execution_engine = ExecutionEngine.create the_module_provider in
1504 let the_fpm = PassManager.create_function the_module_provider in
1503 let the_execution_engine = ExecutionEngine.create Codegen.the_module in
1504 let the_fpm = PassManager.create_function Codegen.the_module in
15051505
15061506 (* Set up the optimizer pipeline. Start with registering info about how the
15071507 * target lays out data structures. *)
15081508 TargetData.add (ExecutionEngine.target_data the_execution_engine) the_fpm;
15091509
15101510 (* Do simple "peephole" optimizations and bit-twiddling optzn. *)
1511 add_instruction_combining the_fpm;
1511 add_instruction_combination the_fpm;
15121512
15131513 (* reassociate expressions. *)
15141514 add_reassociation the_fpm;
542542

                  
                
543543 let main () =
544544 ...
545 let the_fpm = PassManager.create_function the_module_provider in
545 let the_fpm = PassManager.create_function Codegen.the_module in
546546
547547 (* Set up the optimizer pipeline. Start with registering info about how the
548548 * target lays out data structures. *)
13871387 let the_module = create_module context "my cool jit"
13881388 let builder = builder context
13891389 let named_values:(string, llvalue) Hashtbl.t = Hashtbl.create 10
1390 let double_type = double_type context
13901391
13911392 (* Create an alloca instruction in the entry block of the function. This
13921393 * is used for mutable variables etc. *)
14811482 let start_bb = insertion_block builder in
14821483 let the_function = block_parent start_bb in
14831484
1484 let then_bb = append_block "then" the_function in
1485 let then_bb = append_block context "then" the_function in
14851486
14861487 (* Emit 'then' value. *)
14871488 position_at_end then_bb builder;
14931494 let new_then_bb = insertion_block builder in
14941495
14951496 (* Emit 'else' value. *)
1496 let else_bb = append_block "else" the_function in
1497 let else_bb = append_block context "else" the_function in
14971498 position_at_end else_bb builder;
14981499 let else_val = codegen_expr else_ in
14991500
15021503 let new_else_bb = insertion_block builder in
15031504
15041505 (* Emit merge block. *)
1505 let merge_bb = append_block "ifcont" the_function in
1506 let merge_bb = append_block context "ifcont" the_function in
15061507 position_at_end merge_bb builder;
15071508 let incoming = [(then_val, new_then_bb); (else_val, new_else_bb)] in
15081509 let phi = build_phi incoming "iftmp" builder in
15541555
15551556 (* Make the new basic block for the loop header, inserting after current
15561557 * block. *)
1557 let loop_bb = append_block "loop" the_function in
1558 let loop_bb = append_block context "loop" the_function in
15581559
15591560 (* Insert an explicit fall through from the current block to the
15601561 * loop_bb. *)
15981599 let end_cond = build_fcmp Fcmp.One end_cond zero "loopcond" builder in
15991600
16001601 (* Create the "after loop" block and insert it. *)
1601 let after_bb = append_block "afterloop" the_function in
1602 let after_bb = append_block context "afterloop" the_function in
16021603
16031604 (* Insert the conditional branch into the end of loop_end_bb. *)
16041605 ignore (build_cond_br end_cond loop_bb after_bb builder);
17221723 end;
17231724
17241725 (* Create a new basic block to start insertion into. *)
1725 let bb = append_block "entry" the_function in
1726 let bb = append_block context "entry" the_function in
17261727 position_at_end bb builder;
17271728
17281729 try
17901791 the_execution_engine in
17911792
17921793 print_string "Evaluated to ";
1793 print_float (GenericValue.as_float double_type result);
1794 print_float (GenericValue.as_float Codegen.double_type result);
17941795 print_newline ();
17951796 with Stream.Error s | Codegen.Error s ->
17961797 (* Skip token for error recovery. *)
18301831 let stream = Lexer.lex (Stream.of_channel stdin) in
18311832
18321833 (* Create the JIT. *)
1833 let the_module_provider = ModuleProvider.create Codegen.the_module in
1834 let the_execution_engine = ExecutionEngine.create the_module_provider in
1835 let the_fpm = PassManager.create_function the_module_provider in
1834 let the_execution_engine = ExecutionEngine.create Codegen.the_module in
1835 let the_fpm = PassManager.create_function Codegen.the_module in
18361836
18371837 (* Set up the optimizer pipeline. Start with registering info about how the
18381838 * target lays out data structures. *)
18421842 add_memory_to_register_promotion the_fpm;
18431843
18441844 (* Do simple "peephole" optimizations and bit-twiddling optzn. *)
1845 add_instruction_combining the_fpm;
1845 add_instruction_combination the_fpm;
18461846
18471847 (* reassociate expressions. *)
18481848 add_reassociation the_fpm;