Chris@42: (* Chris@42: * Copyright (c) 1997-1999 Massachusetts Institute of Technology Chris@42: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@42: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology Chris@42: * Chris@42: * This program is free software; you can redistribute it and/or modify Chris@42: * it under the terms of the GNU General Public License as published by Chris@42: * the Free Software Foundation; either version 2 of the License, or Chris@42: * (at your option) any later version. Chris@42: * Chris@42: * This program is distributed in the hope that it will be useful, Chris@42: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@42: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@42: * GNU General Public License for more details. Chris@42: * Chris@42: * You should have received a copy of the GNU General Public License Chris@42: * along with this program; if not, write to the Free Software Chris@42: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@42: * Chris@42: *) Chris@42: Chris@42: (* Chris@42: * This module contains the definition of a C-like abstract Chris@42: * syntax tree, and functions to convert ML values into C Chris@42: * programs Chris@42: *) Chris@42: Chris@42: open Expr Chris@42: open Annotate Chris@42: open List Chris@42: Chris@42: let realtype = "R" Chris@42: let realtypep = realtype ^ " *" Chris@42: let extended_realtype = "E" Chris@42: let constrealtype = "const " ^ realtype Chris@42: let constrealtypep = constrealtype ^ " *" Chris@42: Chris@42: let stridetype = "stride" Chris@42: Chris@42: (*********************************** Chris@42: * C program structure Chris@42: ***********************************) Chris@42: type c_decl = Chris@42: | Decl of string * string Chris@42: | Tdecl of string (* arbitrary text declaration *) Chris@42: Chris@42: and c_ast = Chris@42: | Asch of annotated_schedule Chris@42: | Simd_leavefun Chris@42: | Return of c_ast Chris@42: | For of c_ast * c_ast * c_ast * c_ast Chris@42: | If of c_ast * c_ast Chris@42: | Block of (c_decl list) * (c_ast list) Chris@42: | Binop of string * c_ast * c_ast Chris@42: | Expr_assign of c_ast * c_ast Chris@42: | Stmt_assign of c_ast * c_ast Chris@42: | Comma of c_ast * c_ast Chris@42: | Integer of int Chris@42: | CVar of string Chris@42: | CCall of string * c_ast Chris@42: | CPlus of c_ast list Chris@42: | ITimes of c_ast * c_ast Chris@42: | CUminus of c_ast Chris@42: and c_fcn = Fcn of string * string * (c_decl list) * c_ast Chris@42: Chris@42: Chris@42: let ctimes = function Chris@42: | (Integer 1), a -> a Chris@42: | a, (Integer 1) -> a Chris@42: | a, b -> ITimes (a, b) Chris@42: Chris@42: (* Chris@42: * C AST unparser Chris@42: *) Chris@42: let foldr_string_concat l = fold_right (^) l "" Chris@42: Chris@42: let rec unparse_expr_c = Chris@42: let yes x = x and no x = "" in Chris@42: Chris@42: let rec unparse_plus maybe = Chris@42: let maybep = maybe " + " in Chris@42: function Chris@42: | [] -> "" Chris@42: | (Uminus (Times (a, b))) :: (Uminus c) :: d -> Chris@42: maybep ^ (op "FNMA" a b c) ^ (unparse_plus yes d) Chris@42: | (Uminus c) :: (Uminus (Times (a, b))) :: d -> Chris@42: maybep ^ (op "FNMA" a b c) ^ (unparse_plus yes d) Chris@42: | (Uminus (Times (a, b))) :: c :: d -> Chris@42: maybep ^ (op "FNMS" a b c) ^ (unparse_plus yes d) Chris@42: | c :: (Uminus (Times (a, b))) :: d -> Chris@42: maybep ^ (op "FNMS" a b c) ^ (unparse_plus yes d) Chris@42: | (Times (a, b)) :: (Uminus c) :: d -> Chris@42: maybep ^ (op "FMS" a b c) ^ (unparse_plus yes d) Chris@42: | (Uminus c) :: (Times (a, b)) :: d -> Chris@42: maybep ^ (op "FMS" a b c) ^ (unparse_plus yes d) Chris@42: | (Times (a, b)) :: c :: d -> Chris@42: maybep ^ (op "FMA" a b c) ^ (unparse_plus yes d) Chris@42: | c :: (Times (a, b)) :: d -> Chris@42: maybep ^ (op "FMA" a b c) ^ (unparse_plus yes d) Chris@42: | (Uminus a :: b) -> Chris@42: " - " ^ (parenthesize a) ^ (unparse_plus yes b) Chris@42: | (a :: b) -> Chris@42: maybep ^ (parenthesize a) ^ (unparse_plus yes b) Chris@42: and parenthesize x = match x with Chris@42: | (Load _) -> unparse_expr_c x Chris@42: | (Num _) -> unparse_expr_c x Chris@42: | _ -> "(" ^ (unparse_expr_c x) ^ ")" Chris@42: and op nam a b c = Chris@42: nam ^ "(" ^ (unparse_expr_c a) ^ ", " ^ (unparse_expr_c b) ^ ", " ^ Chris@42: (unparse_expr_c c) ^ ")" Chris@42: Chris@42: in function Chris@42: | Load v -> Variable.unparse v Chris@42: | Num n -> Number.to_konst n Chris@42: | Plus [] -> "0.0 /* bug */" Chris@42: | Plus [a] -> " /* bug */ " ^ (unparse_expr_c a) Chris@42: | Plus a -> (unparse_plus no a) Chris@42: | Times (a, b) -> (parenthesize a) ^ " * " ^ (parenthesize b) Chris@42: | Uminus (Plus [a; Uminus b]) -> unparse_plus no [b; Uminus a] Chris@42: | Uminus a -> "- " ^ (parenthesize a) Chris@42: | _ -> failwith "unparse_expr_c" Chris@42: Chris@42: and unparse_expr_generic = Chris@42: let rec u x = unparse_expr_generic x Chris@42: and unary op a = Printf.sprintf "%s(%s)" op (u a) Chris@42: and binary op a b = Printf.sprintf "%s(%s, %s)" op (u a) (u b) Chris@42: and ternary op a b c = Printf.sprintf "%s(%s, %s, %s)" op (u a) (u b) (u c) Chris@42: and quaternary op a b c d = Chris@42: Printf.sprintf "%s(%s, %s, %s, %s)" op (u a) (u b) (u c) (u d) Chris@42: and unparse_plus = function Chris@42: | [(Uminus (Times (a, b))); Times (c, d)] -> quaternary "FNMMS" a b c d Chris@42: | [Times (c, d); (Uminus (Times (a, b)))] -> quaternary "FNMMS" a b c d Chris@42: | [Times (c, d); (Times (a, b))] -> quaternary "FMMA" a b c d Chris@42: | [(Uminus (Times (a, b))); c] -> ternary "FNMS" a b c Chris@42: | [c; (Uminus (Times (a, b)))] -> ternary "FNMS" a b c Chris@42: | [(Uminus c); (Times (a, b))] -> ternary "FMS" a b c Chris@42: | [(Times (a, b)); (Uminus c)] -> ternary "FMS" a b c Chris@42: | [c; (Times (a, b))] -> ternary "FMA" a b c Chris@42: | [(Times (a, b)); c] -> ternary "FMA" a b c Chris@42: | [a; Uminus b] -> binary "SUB" a b Chris@42: | [a; b] -> binary "ADD" a b Chris@42: | a :: b :: c -> binary "ADD" a (Plus (b :: c)) Chris@42: | _ -> failwith "unparse_plus" Chris@42: in function Chris@42: | Load v -> Variable.unparse v Chris@42: | Num n -> Number.to_konst n Chris@42: | Plus a -> unparse_plus a Chris@42: | Times (a, b) -> binary "MUL" a b Chris@42: | Uminus a -> unary "NEG" a Chris@42: | _ -> failwith "unparse_expr" Chris@42: Chris@42: and unparse_expr x = Chris@42: if !Magic.generic_arith then Chris@42: unparse_expr_generic x Chris@42: else Chris@42: unparse_expr_c x Chris@42: Chris@42: and unparse_assignment (Assign (v, x)) = Chris@42: (Variable.unparse v) ^ " = " ^ (unparse_expr x) ^ ";\n" Chris@42: Chris@42: and unparse_annotated force_bracket = Chris@42: let rec unparse_code = function Chris@42: ADone -> "" Chris@42: | AInstr i -> unparse_assignment i Chris@42: | ASeq (a, b) -> Chris@42: (unparse_annotated false a) ^ (unparse_annotated false b) Chris@42: and declare_variables l = Chris@42: let rec uvar = function Chris@42: [] -> failwith "uvar" Chris@42: | [v] -> (Variable.unparse v) ^ ";\n" Chris@42: | a :: b -> (Variable.unparse a) ^ ", " ^ (uvar b) Chris@42: in let rec vvar l = Chris@42: let s = if !Magic.compact then 15 else 1 in Chris@42: if (List.length l <= s) then Chris@42: match l with Chris@42: [] -> "" Chris@42: | _ -> extended_realtype ^ " " ^ (uvar l) Chris@42: else Chris@42: (vvar (Util.take s l)) ^ (vvar (Util.drop s l)) Chris@42: in vvar (List.filter Variable.is_temporary l) Chris@42: in function Chris@42: Annotate (_, _, decl, _, code) -> Chris@42: if (not force_bracket) && (Util.null decl) then Chris@42: unparse_code code Chris@42: else "{\n" ^ Chris@42: (declare_variables decl) ^ Chris@42: (unparse_code code) ^ Chris@42: "}\n" Chris@42: Chris@42: and unparse_decl = function Chris@42: | Decl (a, b) -> a ^ " " ^ b ^ ";\n" Chris@42: | Tdecl x -> x Chris@42: Chris@42: and unparse_ast = Chris@42: let rec unparse_plus = function Chris@42: | [] -> "" Chris@42: | (CUminus a :: b) -> " - " ^ (parenthesize a) ^ (unparse_plus b) Chris@42: | (a :: b) -> " + " ^ (parenthesize a) ^ (unparse_plus b) Chris@42: and parenthesize x = match x with Chris@42: | (CVar _) -> unparse_ast x Chris@42: | (CCall _) -> unparse_ast x Chris@42: | (Integer _) -> unparse_ast x Chris@42: | _ -> "(" ^ (unparse_ast x) ^ ")" Chris@42: Chris@42: in Chris@42: function Chris@42: | Asch a -> (unparse_annotated true a) Chris@42: | Simd_leavefun -> "" (* used only in SIMD code *) Chris@42: | Return x -> "return " ^ unparse_ast x ^ ";" Chris@42: | For (a, b, c, d) -> Chris@42: "for (" ^ Chris@42: unparse_ast a ^ "; " ^ unparse_ast b ^ "; " ^ unparse_ast c Chris@42: ^ ")" ^ unparse_ast d Chris@42: | If (a, d) -> Chris@42: "if (" ^ Chris@42: unparse_ast a Chris@42: ^ ")" ^ unparse_ast d Chris@42: | Block (d, s) -> Chris@42: if (s == []) then "" Chris@42: else Chris@42: "{\n" ^ Chris@42: foldr_string_concat (map unparse_decl d) ^ Chris@42: foldr_string_concat (map unparse_ast s) ^ Chris@42: "}\n" Chris@42: | Binop (op, a, b) -> (unparse_ast a) ^ op ^ (unparse_ast b) Chris@42: | Expr_assign (a, b) -> (unparse_ast a) ^ " = " ^ (unparse_ast b) Chris@42: | Stmt_assign (a, b) -> (unparse_ast a) ^ " = " ^ (unparse_ast b) ^ ";\n" Chris@42: | Comma (a, b) -> (unparse_ast a) ^ ", " ^ (unparse_ast b) Chris@42: | Integer i -> string_of_int i Chris@42: | CVar s -> s Chris@42: | CCall (s, x) -> s ^ "(" ^ (unparse_ast x) ^ ")" Chris@42: | CPlus [] -> "0 /* bug */" Chris@42: | CPlus [a] -> " /* bug */ " ^ (unparse_ast a) Chris@42: | CPlus (a::b) -> (parenthesize a) ^ (unparse_plus b) Chris@42: | ITimes (a, b) -> (parenthesize a) ^ " * " ^ (parenthesize b) Chris@42: | CUminus a -> "- " ^ (parenthesize a) Chris@42: Chris@42: and unparse_function = function Chris@42: Fcn (typ, name, args, body) -> Chris@42: let rec unparse_args = function Chris@42: [Decl (a, b)] -> a ^ " " ^ b Chris@42: | (Decl (a, b)) :: s -> a ^ " " ^ b ^ ", " Chris@42: ^ unparse_args s Chris@42: | [] -> "" Chris@42: | _ -> failwith "unparse_function" Chris@42: in Chris@42: (typ ^ " " ^ name ^ "(" ^ unparse_args args ^ ")\n" ^ Chris@42: unparse_ast body) Chris@42: Chris@42: Chris@42: (************************************************************* Chris@42: * traverse a a function and return a list of all expressions, Chris@42: * in the execution order Chris@42: **************************************************************) Chris@42: let rec fcn_to_expr_list = fun (Fcn (_, _, _, body)) -> ast_to_expr_list body Chris@42: and acode_to_expr_list = function Chris@42: AInstr (Assign (_, x)) -> [x] Chris@42: | ASeq (a, b) -> Chris@42: (asched_to_expr_list a) @ (asched_to_expr_list b) Chris@42: | _ -> [] Chris@42: and asched_to_expr_list (Annotate (_, _, _, _, code)) = Chris@42: acode_to_expr_list code Chris@42: and ast_to_expr_list = function Chris@42: Asch a -> asched_to_expr_list a Chris@42: | Block (_, a) -> flatten (map ast_to_expr_list a) Chris@42: | For (_, _, _, body) -> ast_to_expr_list body Chris@42: | If (_, body) -> ast_to_expr_list body Chris@42: | _ -> [] Chris@42: Chris@42: (*********************** Chris@42: * Extracting Constants Chris@42: ***********************) Chris@42: Chris@42: (* add a new key & value to a list of (key,value) pairs, where Chris@42: the keys are floats and each key is unique up to almost_equal *) Chris@42: Chris@42: let extract_constants f = Chris@42: let constlist = flatten (map expr_to_constants (ast_to_expr_list f)) Chris@42: in map Chris@42: (fun n -> Chris@42: Tdecl Chris@42: ("DK(" ^ (Number.to_konst n) ^ ", " ^ (Number.to_string n) ^ Chris@42: ");\n")) Chris@42: (unique_constants constlist) Chris@42: Chris@42: (****************************** Chris@42: Extracting operation counts Chris@42: ******************************) Chris@42: Chris@42: let count_stack_vars = Chris@42: let rec count_acode = function Chris@42: | ASeq (a, b) -> max (count_asched a) (count_asched b) Chris@42: | _ -> 0 Chris@42: and count_asched (Annotate (_, _, decl, _, code)) = Chris@42: (length decl) + (count_acode code) Chris@42: and count_ast = function Chris@42: | Asch a -> count_asched a Chris@42: | Block (d, a) -> (length d) + (Util.max_list (map count_ast a)) Chris@42: | For (_, _, _, body) -> count_ast body Chris@42: | If (_, body) -> count_ast body Chris@42: | _ -> 0 Chris@42: in function (Fcn (_, _, _, body)) -> count_ast body Chris@42: Chris@42: let count_memory_acc f = Chris@42: let rec count_var v = Chris@42: if (Variable.is_locative v) then 1 else 0 Chris@42: and count_acode = function Chris@42: | AInstr (Assign (v, _)) -> count_var v Chris@42: | ASeq (a, b) -> (count_asched a) + (count_asched b) Chris@42: | _ -> 0 Chris@42: and count_asched = function Chris@42: Annotate (_, _, _, _, code) -> count_acode code Chris@42: and count_ast = function Chris@42: | Asch a -> count_asched a Chris@42: | Block (_, a) -> (Util.sum_list (map count_ast a)) Chris@42: | Comma (a, b) -> (count_ast a) + (count_ast b) Chris@42: | For (_, _, _, body) -> count_ast body Chris@42: | If (_, body) -> count_ast body Chris@42: | _ -> 0 Chris@42: and count_acc_expr_func acc = function Chris@42: | Load v -> acc + (count_var v) Chris@42: | Plus a -> fold_left count_acc_expr_func acc a Chris@42: | Times (a, b) -> fold_left count_acc_expr_func acc [a; b] Chris@42: | Uminus a -> count_acc_expr_func acc a Chris@42: | _ -> acc Chris@42: in let (Fcn (typ, name, args, body)) = f Chris@42: in (count_ast body) + Chris@42: fold_left count_acc_expr_func 0 (fcn_to_expr_list f) Chris@42: Chris@42: let good_for_fma = To_alist.good_for_fma Chris@42: Chris@42: let build_fma = function Chris@42: | [a; Times (b, c)] when good_for_fma (b, c) -> Some (a, b, c) Chris@42: | [Times (b, c); a] when good_for_fma (b, c) -> Some (a, b, c) Chris@42: | [a; Uminus (Times (b, c))] when good_for_fma (b, c) -> Some (a, b, c) Chris@42: | [Uminus (Times (b, c)); a] when good_for_fma (b, c) -> Some (a, b, c) Chris@42: | _ -> None Chris@42: Chris@42: let rec count_flops_expr_func (adds, mults, fmas) = function Chris@42: | Plus [] -> (adds, mults, fmas) Chris@42: | Plus ([_; _] as a) -> Chris@42: begin Chris@42: match build_fma a with Chris@42: | None -> Chris@42: fold_left count_flops_expr_func Chris@42: (adds + (length a) - 1, mults, fmas) a Chris@42: | Some (a, b, c) -> Chris@42: fold_left count_flops_expr_func (adds, mults, fmas+1) [a; b; c] Chris@42: end Chris@42: | Plus (a :: b) -> Chris@42: count_flops_expr_func (adds, mults, fmas) (Plus [a; Plus b]) Chris@42: | Times (NaN MULTI_A,_) -> (adds, mults, fmas) Chris@42: | Times (NaN MULTI_B,_) -> (adds, mults, fmas) Chris@42: | Times (NaN I,b) -> count_flops_expr_func (adds, mults, fmas) b Chris@42: | Times (NaN CONJ,b) -> count_flops_expr_func (adds, mults, fmas) b Chris@42: | Times (a,b) -> fold_left count_flops_expr_func (adds, mults+1, fmas) [a; b] Chris@42: | CTimes (a,b) -> Chris@42: fold_left count_flops_expr_func (adds+1, mults+2, fmas) [a; b] Chris@42: | CTimesJ (a,b) -> Chris@42: fold_left count_flops_expr_func (adds+1, mults+2, fmas) [a; b] Chris@42: | Uminus a -> count_flops_expr_func (adds, mults, fmas) a Chris@42: | _ -> (adds, mults, fmas) Chris@42: Chris@42: let count_flops f = Chris@42: fold_left count_flops_expr_func (0, 0, 0) (fcn_to_expr_list f) Chris@42: Chris@42: let count_constants f = Chris@42: length (unique_constants (flatten (map expr_to_constants (fcn_to_expr_list f)))) Chris@42: Chris@42: let arith_complexity f = Chris@42: let (a, m, fmas) = count_flops f Chris@42: and v = count_stack_vars f Chris@42: and c = count_constants f Chris@42: and mem = count_memory_acc f Chris@42: in (a, m, fmas, v, c, mem) Chris@42: Chris@42: (* print the operation costs *) Chris@42: let print_cost f = Chris@42: let Fcn (_, _, _, _) = f Chris@42: and (a, m, fmas, v, c, mem) = arith_complexity f Chris@42: in Chris@42: "/*\n"^ Chris@42: " * This function contains " ^ Chris@42: (string_of_int (a + fmas)) ^ " FP additions, " ^ Chris@42: (string_of_int (m + fmas)) ^ " FP multiplications,\n" ^ Chris@42: " * (or, " ^ Chris@42: (string_of_int a) ^ " additions, " ^ Chris@42: (string_of_int m) ^ " multiplications, " ^ Chris@42: (string_of_int fmas) ^ " fused multiply/add),\n" ^ Chris@42: " * " ^ (string_of_int v) ^ " stack variables, " ^ Chris@42: (string_of_int c) ^ " constants, and " ^ Chris@42: (string_of_int mem) ^ " memory accesses\n" ^ Chris@42: " */\n" Chris@42: Chris@42: (***************************************** Chris@42: * functions that create C arrays Chris@42: *****************************************) Chris@42: type stride = Chris@42: | SVar of string Chris@42: | SConst of string Chris@42: | SInteger of int Chris@42: | SNeg of stride Chris@42: Chris@42: type sstride = Chris@42: | Simple of int Chris@42: | Constant of (string * int) Chris@42: | Composite of (string * int) Chris@42: | Negative of sstride Chris@42: Chris@42: let rec simplify_stride stride i = Chris@42: match (stride, i) with Chris@42: (_, 0) -> Simple 0 Chris@42: | (SInteger n, i) -> Simple (n * i) Chris@42: | (SConst s, i) -> Constant (s, i) Chris@42: | (SVar s, i) -> Composite (s, i) Chris@42: | (SNeg x, i) -> Chris@42: match (simplify_stride x i) with Chris@42: | Negative y -> y Chris@42: | y -> Negative y Chris@42: Chris@42: let rec cstride_to_string = function Chris@42: | Simple i -> string_of_int i Chris@42: | Constant (s, i) -> Chris@42: if !Magic.lisp_syntax then Chris@42: "(* " ^ s ^ " " ^ (string_of_int i) ^ ")" Chris@42: else Chris@42: s ^ " * " ^ (string_of_int i) Chris@42: | Composite (s, i) -> Chris@42: if !Magic.lisp_syntax then Chris@42: "(* " ^ s ^ " " ^ (string_of_int i) ^ ")" Chris@42: else Chris@42: "WS(" ^ s ^ ", " ^ (string_of_int i) ^ ")" Chris@42: | Negative x -> "-" ^ cstride_to_string x Chris@42: Chris@42: let aref name index = Chris@42: if !Magic.lisp_syntax then Chris@42: Printf.sprintf "(aref %s %s)" name index Chris@42: else Chris@42: Printf.sprintf "%s[%s]" name index Chris@42: Chris@42: let array_subscript name stride k = Chris@42: aref name (cstride_to_string (simplify_stride stride k)) Chris@42: Chris@42: let varray_subscript name vstride stride v i = Chris@42: let vindex = simplify_stride vstride v Chris@42: and iindex = simplify_stride stride i Chris@42: in Chris@42: let index = Chris@42: match (vindex, iindex) with Chris@42: (Simple vi, Simple ii) -> string_of_int (vi + ii) Chris@42: | (Simple 0, x) -> cstride_to_string x Chris@42: | (x, Simple 0) -> cstride_to_string x Chris@42: | _ -> (cstride_to_string vindex) ^ " + " ^ (cstride_to_string iindex) Chris@42: in aref name index Chris@42: Chris@42: let real_of s = "c_re(" ^ s ^ ")" Chris@42: let imag_of s = "c_im(" ^ s ^ ")" Chris@42: Chris@42: let flops_of f = Chris@42: let (add, mul, fma) = count_flops f in Chris@42: Printf.sprintf "{ %d, %d, %d, 0 }" add mul fma