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