Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Diff of /branches/lamont/src/compiler/cl-target/cl-target.sml
ViewVC logotype

Diff of /branches/lamont/src/compiler/cl-target/cl-target.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2297, Fri Mar 15 22:05:31 2013 UTC revision 2298, Fri Mar 15 22:18:22 2013 UTC
# Line 13  Line 13 
13      structure CL = CLang      structure CL = CLang
14      structure RN = RuntimeNames      structure RN = RuntimeNames
15      structure N = CNames      structure N = CNames
16        structure ToC = TreeToC
17      structure ToCL = TreeToCL      structure ToCL = TreeToCL
     structure SU = StrandUtil  
18      structure HF = CLHeaderFrag      structure HF = CLHeaderFrag
19      structure SF = CLSchedFrag      structure SF = CLSchedFrag
20    
# Line 101  Line 101 
101      fun lvalueSV name = CL.mkIndirect(CL.mkVar "selfOut", name)      fun lvalueSV name = CL.mkIndirect(CL.mkVar "selfOut", name)
102      fun rvalueSV name = CL.mkIndirect(CL.mkVar "selfIn", name)      fun rvalueSV name = CL.mkIndirect(CL.mkVar "selfIn", name)
103    
   (* C variable translation *)  
     structure TrCVar =  
       struct  
         type env = CL.typed_var TreeIL.Var.Map.map  
         fun lookup (env, x) = (case V.Map.find (env, x)  
                of SOME(CL.V(_, x')) => x'  
                 | NONE => raise Fail(concat["TrCVar.lookup(_, ", V.name x, ")"])  
               (* end case *))  
       (* translate a variable that occurs in an l-value context (i.e., as the target of an assignment) *)  
         fun lvalueVar (env, x) = (case V.kind x  
                of IL.VK_Local => CL.mkVar(lookup(env, x))  
                 | _ => CL.mkIndirect(CL.mkVar RN.globalsVarName, lookup(env, x))  
               (* end case *))  
       (* translate a variable that occurs in an r-value context *)  
         fun rvalueVar (env, x) = (case V.kind x  
                of IL.VK_Local => CL.mkVar(lookup(env, x))  
                 | _ => CL.mkIndirect(CL.mkVar RN.globalsVarName, lookup(env, x))  
               (* end case *))  
       (* translate a strand state variable that occurs in an l-value context *)  
         fun lvalueStateVar (IL.SV{name, ...}) = lvalueSV name  
       (* translate a strand state variable that occurs in an r-value context *)  
         fun rvalueStateVar (IL.SV{name, ...}) = rvalueSV name  
       end  
   
     structure ToC = TreeToCFn (TrCVar)  
   
104      type var = CL.typed_var      type var = CL.typed_var
105      type exp = CL.exp      type exp = CL.exp
106      type stm = CL.stm      type stm = CL.stm
# Line 165  Line 139 
139        }        }
140    
141      datatype program = Prog of {      datatype program = Prog of {
142          tgt : TargetUtil.target_desc,   (* info about target *)          props : Properties.props,
         hasDie : bool,                  (* true for programs that have "die" *)  
         hasNew : bool,                  (* true for programs that have "new" *)  
         hasCom : bool,                  (* true for programs that have strand communication *)  
         hasReduce : bool,               (* true for programs that have global reduce *)  
143          globals : mirror_var list ref,          globals : mirror_var list ref,
144          topDecls : CL.decl list ref,          topDecls : CL.decl list ref,
145          strands : strand AtomTable.hash_table,          strands : strand AtomTable.hash_table,
# Line 226  Line 196 
196                  | InitiallyScope => ToCL.trBlock (vMap, blk)                  | InitiallyScope => ToCL.trBlock (vMap, blk)
197                  | _ => ToC.trBlock (vMap, blk)                  | _ => ToC.trBlock (vMap, blk)
198                (* end case *))                (* end case *))
   
   
199          fun free (ENV{vMap, ...}, blk) = ToC.trFree (vMap, blk)          fun free (ENV{vMap, ...}, blk) = ToC.trFree (vMap, blk)
200          fun exp (ENV{vMap, ...}, e) = ToCL.trExp(vMap, e)          fun exp (ENV{vMap, ...}, e) = ToCL.trExp(vMap, e)
201        end        end
# Line 268  Line 236 
236                  vMap = V.Map.empty,                  vMap = V.Map.empty,
237                  scope = NoScope                  scope = NoScope
238                }                }
       (* define the current translation context *)  
         fun setScope scope (ENV{info, vMap, ...}) = ENV{info=info, vMap=vMap, scope=scope}  
         val scopeGlobal = setScope GlobalScope  
         val scopeInitially = setScope InitiallyScope  
         fun scopeStrand env = setScope StrandScope env  
         fun scopeMethod (env, name) = setScope (MethodScope name) env  
239        (* bind a TreeIL varaiable to a target variable *)        (* bind a TreeIL varaiable to a target variable *)
240          fun bind (ENV{info, vMap, scope}, x, x') = ENV{          fun bind (ENV{info, vMap, scope}, x, x') = ENV{
241                  info = info,                  info = info,
242                  vMap = V.Map.insert(vMap, x, x'),                  vMap = V.Map.insert(vMap, x, x'),
243                  scope = scope                  scope = scope
244                }                }
245          (* define the current translation context *)
246            fun setScope (scope, glob, selfIn, selfOut) (ENV{info, vMap, ...}) = ENV{
247                    info = info,
248                    vMap = List.foldl
249                        (fn ((x, x'), vm) => V.Map.insert(vm, x, x'))
250                          vMap [
251                              (PseudoVars.global, CL.V(CL.voidTy, glob)),
252                              (PseudoVars.selfIn, CL.V(CL.voidTy, selfIn)),
253                              (PseudoVars.selfOut, CL.V(CL.voidTy, selfOut))
254                            ],
255                    scope = scope
256                  }
257          (* define the current translation context *)
258            val scopeGlobal = setScope (GlobalScope, RN.globalsVarName, "_bogus_", "_bogus_")
259            val scopeInitially =
260                  setScope (InitiallyScope, RN.globalsVarName, "_bogus_", "_bogus_")
261            val scopeStrand = setScope (StrandScope, RN.globalsVarName, "selfIn", "selfOut")
262            fun scopeMethod (env, name) =
263                  setScope (MethodScope name, RN.globalsVarName, "selfIn", "selfOut") env
264        end        end
265    
266    (* strands *)    (* strands *)
# Line 330  Line 311 
311                end                end
312    
313        (* register a strand method *)        (* register a strand method *)
314          fun method (Strand{name, tyName, code,...}, methName, body,hasCom) = let          fun method (Strand{name, tyName, code,...}, methName, body) = let
315                val params = [                val params = [
316                        globalParam (CL.T_Ptr(CL.T_Named tyName), "selfIn"),                        globalParam (CL.T_Ptr(CL.T_Named tyName), "selfIn"),
317                        globalParam (CL.T_Ptr(CL.T_Named tyName), "selfOut"),                        globalParam (CL.T_Ptr(CL.T_Named tyName), "selfOut"),
# Line 355  Line 336 
336                RN.initTargetSpec (#double tgt);                RN.initTargetSpec (#double tgt);
337                CNames.initTargetSpec {double = #double tgt, long = false};                CNames.initTargetSpec {double = #double tgt, long = false};
338                Prog{                Prog{
339                    tgt = tgt,                    props = Properties.mkProps (tgt, props),
                   hasDie = SU.hasProp SU.StrandsMayDie props,  
                   hasNew = SU.hasProp SU.NewStrands props,  
                   hasCom = SU.hasProp SU.StrandCommunication props,  
                   hasReduce = SU.hasProp SU.GlobalReduce props,  
340                    globals = ref [],                    globals = ref [],
341                    topDecls = ref [],                    topDecls = ref [],
342                    strands = AtomTable.mkTable (16, Fail "strand table"),                    strands = AtomTable.mkTable (16, Fail "strand table"),
# Line 369  Line 346 
346                    prFn = ref(CL.D_Comment(["No Print Function"])),                    prFn = ref(CL.D_Comment(["No Print Function"])),
347                    outFn = ref(CL.D_Comment(["No Output Function"]))                    outFn = ref(CL.D_Comment(["No Output Function"]))
348                  })                  })
349    
350        (* generates the global reduction function *)        (* generates the global reduction function *)
351         fun genGlobalFun(Prog{topDecls,...},_, reductionBlock) = ()         fun genGlobalFun(Prog{topDecls,...},_, reductionBlock) = ()
352    
# Line 409  Line 387 
387                  topDecls := shutdownFn :: initFn :: initHelperFn :: !topDecls                  topDecls := shutdownFn :: initFn :: initHelperFn :: !topDecls
388                end                end
389        (* register the global destruction part of a program *)        (* register the global destruction part of a program *)
390          fun free (Prog{tgt, topDecls, ...}, free) = let          fun free (Prog{props, topDecls, ...}, free) = let
391                val worldTy = CL.T_Ptr(CL.T_Named(N.worldTy tgt))                val worldTy = CL.T_Ptr(CL.T_Named(N.worldTy props))
392                val globTy = CL.T_Ptr(CL.T_Named(N.globalTy tgt))                val globTy = CL.T_Ptr(CL.T_Named(N.globalTy props))
393                val free = CL.mkBlock(                val free = CL.mkBlock(
394                      CL.mkDeclInit(globTy, "glob", CL.mkIndirect(CL.mkVar "wrld", "globals")) ::                      CL.mkDeclInit(globTy, "glob", CL.mkIndirect(CL.mkVar "wrld", "globals")) ::
395                      CL.unBlock free @ [CL.mkReturn(SOME(CL.mkVar "false"))])                      CL.unBlock free @ [CL.mkReturn(SOME(CL.mkVar "false"))])
# Line 425  Line 403 
403    
404        (* create and register the initially function for a program *)        (* create and register the initially function for a program *)
405          fun initially {          fun initially {
406                prog = Prog{tgt, strands, initially, numDims, ...},                prog = Prog{props, strands, initially, numDims, ...},
407                isArray : bool,                isArray : bool,
408                iterPrefix : stm list,                iterPrefix : stm list,
409                iters : (var * exp * exp) list,                iters : (var * exp * exp) list,
# Line 435  Line 413 
413              } = let              } = let
414                val name = Atom.toString strand                val name = Atom.toString strand
415                val nDims = List.length iters                val nDims = List.length iters
416                val worldTy = CL.T_Ptr(CL.T_Named(N.worldTy tgt))                val worldTy = CL.T_Ptr(CL.T_Named(N.worldTy props))
417                fun mapi f xs = let                fun mapi f xs = let
418                      fun mapf (_, []) = []                      fun mapf (_, []) = []
419                        | mapf (i, x::xs) = f(i, x) :: mapf(i+1, xs)                        | mapf (i, x::xs) = f(i, x) :: mapf(i+1, xs)
# Line 493  Line 471 
471                      allocCode @                      allocCode @
472                      iterCode @                      iterCode @
473                      [CL.mkReturn(SOME(CL.mkVar "wrld"))])                      [CL.mkReturn(SOME(CL.mkVar "wrld"))])
474                val initFn = CL.D_Func([], worldTy, N.initially tgt, [], body)                val initFn = CL.D_Func([], worldTy, N.initially props, [], body)
475                in                in
476                  numDims := nDims;                  numDims := nDims;
477                  initially := initFn                  initially := initFn
# Line 691  Line 669 
669                          String.concat("Diderot_data_ptr_t diderotDataPtrs;\n" :: imageDataStms)))                          String.concat("Diderot_data_ptr_t diderotDataPtrs;\n" :: imageDataStms)))
670                      ]                      ]
671                in                in
672                  CL.verbatim [CLUpdateFrag.text] placeHolders                  CL.verbatimDcl [CLUpdateFrag.text] placeHolders
673                end                end
674    
675          fun genStrandCopy(Strand{tyName,name,state,...}) = let          fun genStrandCopy(Strand{tyName,name,state,...}) = let
# Line 796  Line 774 
774    
775          fun genSrc (baseName, prog) = let          fun genSrc (baseName, prog) = let
776                val Prog{                val Prog{
777                        tgt,                        props,
778                        globals, topDecls, strands, initially,                        globals, topDecls, strands, initially,
779                        imgGlobals, numDims,outFn, ...                        imgGlobals, numDims,outFn, ...
780                      } = prog                      } = prog
# Line 813  Line 791 
791                in                in
792                (* Generate the OpenCL file *)                (* Generate the OpenCL file *)
793                (* Retrieve the header information *)                (* Retrieve the header information *)
794                  clppDecl (CL.verbatim [HF.text] [                  clppDecl (CL.verbatimDcl [HF.text] [
795                      ("OUTFILE", clFileName),                      ("OUTFILE", clFileName),
796                      ("SRCFILE", OS.Path.joinBaseExt{base=baseName, ext=SOME "diderot"}),                      ("SRCFILE", OS.Path.joinBaseExt{base=baseName, ext=SOME "diderot"}),
797                      ("DIDEROT_FLOAT_PRECISION", TargetUtil.floatPrecisionDef tgt),                      ("DIDEROT_FLOAT_PRECISION", Properties.floatPrecisionDef props),
798                      ("DIDEROT_INT_PRECISION", TargetUtil.intPrecisionDef tgt)                      ("DIDEROT_INT_PRECISION", Properties.intPrecisionDef props)
799                    ]);                    ]);
800                (* if there are no globals, then define a dummy type *)                (* if there are no globals, then define a dummy type *)
801                  if List.null(!globals)                  if List.null(!globals)
# Line 839  Line 817 
817                  clppDecl (genUpdateMethod(strand, globals, imgGlobals));                  clppDecl (genUpdateMethod(strand, globals, imgGlobals));
818                (* Generate the Host C file *)                (* Generate the Host C file *)
819                  cppDecl (CL.D_Verbatim[                  cppDecl (CL.D_Verbatim[
820                      concat["#define " ^ TargetUtil.floatPrecisionDef tgt, "\n"],                      concat["#define " ^ Properties.floatPrecisionDef props, "\n"],
821                      concat["#define " ^ TargetUtil.intPrecisionDef tgt, "\n"],                      concat["#define " ^ Properties.intPrecisionDef props, "\n"],
822                      concat["#define " ^ TargetUtil.targetDef tgt, "\n"],                      concat["#define " ^ Properties.targetDef props, "\n"],
823                      "#include \"Diderot/diderot.h\"\n"                      "#include \"Diderot/diderot.h\"\n"
824                    ]);                    ]);
825                  case !globals                  case !globals
# Line 849  Line 827 
827                    | _ => ()                    | _ => ()
828                  (* end case *);                  (* end case *);
829                  cppDecl (CL.D_Var(["static"], CL.charPtr, "ProgramName",                  cppDecl (CL.D_Var(["static"], CL.charPtr, "ProgramName",
830                    SOME(CL.I_Exp(CL.mkStr(#srcFile tgt)))));                    SOME(CL.I_Exp(CL.mkStr(#srcFile props)))));
831                  cppDecl (genGlobalStruct (#hostTy, !globals, RN.globalsTy));                  cppDecl (genGlobalStruct (#hostTy, !globals, RN.globalsTy));
832                  cppDecl (genGlobalStruct (#shadowTy, !globals, RN.shadowGlobalsTy));                  cppDecl (genGlobalStruct (#shadowTy, !globals, RN.shadowGlobalsTy));
833  (* FIXME: does this really need to be a global? *)  (* FIXME: does this really need to be a global? *)
# Line 872  Line 850 
850                end                end
851    
852        (* output the code to the filesystem.  The string is the basename of the source file *)        (* output the code to the filesystem.  The string is the basename of the source file *)
853          fun generate (prog as Prog{tgt, ...}) = let          fun generate (prog as Prog{props, ...}) = let
854                val {outDir, outBase, exec, double, parallel, debug, ...} = tgt                val {outDir, outBase, exec, double, parallel, debug, ...} = props
855                fun condCons (true, x, xs) = x::xs                fun condCons (true, x, xs) = x::xs
856                  | condCons (false, _, xs) = xs                  | condCons (false, _, xs) = xs
857              (* generate the C compiler flags *)              (* generate the C compiler flags *)
# Line 888  Line 866 
866                val extraLibs = condCons (parallel, #pthread Paths.extraLibs, [])                val extraLibs = condCons (parallel, #pthread Paths.extraLibs, [])
867                val extraLibs = Paths.teemLinkFlags @  #base Paths.extraLibs :: extraLibs                val extraLibs = Paths.teemLinkFlags @  #base Paths.extraLibs :: extraLibs
868                     val extraLibs =  #cl Paths.extraLibs :: extraLibs                     val extraLibs =  #cl Paths.extraLibs :: extraLibs
869                val rtLib = TargetUtil.runtimeName tgt                val rtLib = Properties.runtimeName props
870                val ldOpts = rtLib :: extraLibs                val ldOpts = rtLib :: extraLibs
871                val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}                val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
872                in                in

Legend:
Removed from v.2297  
changed lines
  Added in v.2298

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0