aboutsummaryrefslogtreecommitdiffstats
path: root/src/ortho/llvm35/llvm-executionengine.ads
blob: 72d4cda2f5efe62382609bb2aa24bf83f9c8ffe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
--  LLVM binding
--  Copyright (C) 2014 Tristan Gingold
--
--  GHDL is free software; you can redistribute it and/or modify it under
--  the terms of the GNU General Public License as published by the Free
--  Software Foundation; either version 2, or (at your option) any later
--  version.
--
--  GHDL is distributed in the hope that it will be useful, but WITHOUT ANY
--  WARRANTY; without even the implied warranty of MERCHANTABILITY or
--  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
--  for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with GHDL; see the file COPYING.  If not, write to the Free
--  Software Foundation, 59 Temple Place - Suite 330, Boston, MA
--  02111-1307, USA.
with System; use System;
with Interfaces; use Interfaces;
with Interfaces.C; use Interfaces.C;
with LLVM.Core; use LLVM.Core;
with LLVM.Target; use LLVM.Target;

package LLVM.ExecutionEngine is
   type GenericValueRef is new Address;
   type GenericValueRefArray is array (unsigned range <>) of GenericValueRef;
   pragma Convention (C, GenericValueRefArray);
   type ExecutionEngineRef is new Address;

   procedure LinkInJIT;
   procedure LinkInMCJIT;
   procedure LinkInInterpreter;

   -- Operations on generic values --------------------------------------

   function CreateGenericValueOfInt(Ty : TypeRef;
                                    N : Unsigned_64;
                                    IsSigned : Integer)
                                   return GenericValueRef;

   function CreateGenericValueOfPointer(P : System.Address)
                                           return GenericValueRef;

   function CreateGenericValueOfFloat(Ty : TypeRef; N : double)
                                         return GenericValueRef;

   function GenericValueIntWidth(GenValRef : GenericValueRef)
                                    return unsigned;

   function GenericValueToInt(GenVal : GenericValueRef;
                                  IsSigned : Integer) return Unsigned_64;

   function GenericValueToPointer(GenVal : GenericValueRef)
                                     return System.Address;

   function GenericValueToFloat(TyRef : TypeRef; GenVal : GenericValueRef)
                               return double;

   procedure DisposeGenericValue(GenVal : GenericValueRef);

   -- Operations on execution engines -----------------------------------

   function CreateExecutionEngineForModule
     (EE : access ExecutionEngineRef; M : ModuleRef; Error : access Cstring)
     return Bool;

   function CreateInterpreterForModule (Interp : access ExecutionEngineRef;
                                        M : ModuleRef;
                                        Error : access Cstring)
                                       return Bool;

   function CreateJITCompilerForModule (JIT : access ExecutionEngineRef;
                                        M : ModuleRef;
                                        OptLevel : unsigned;
                                        Error : access Cstring)
     return Bool;


   procedure DisposeExecutionEngine(EE : ExecutionEngineRef);

   procedure RunStaticConstructors(EE : ExecutionEngineRef);

   procedure RunStaticDestructors(EE : ExecutionEngineRef);

   function RunFunctionAsMain(EE : ExecutionEngineRef;
                              F : ValueRef;
                              ArgC : unsigned; Argv : Address; EnvP : Address)
                             return Integer;

   function RunFunction(EE : ExecutionEngineRef;
                        F : ValueRef;
                        NumArgs : unsigned;
                        Args : GenericValueRefArray)
                       return GenericValueRef;

   procedure FreeMachineCodeForFunction(EE : ExecutionEngineRef; F : ValueRef);

   procedure AddModule(EE : ExecutionEngineRef; M : ModuleRef);

   function RemoveModule(EE : ExecutionEngineRef;
                         M : ModuleRef;
                         OutMod : access ModuleRef;
                         OutError : access Cstring) return Bool;

   function FindFunction(EE : ExecutionEngineRef; Name : Cstring;
                                                  OutFn : access ValueRef)
                        return Integer;

   function GetExecutionEngineTargetData(EE : ExecutionEngineRef)
                                        return TargetDataRef;

   procedure AddGlobalMapping(EE : ExecutionEngineRef; Global : ValueRef;
                                                       Addr : Address);

   function GetPointerToGlobal (EE : ExecutionEngineRef; GV : ValueRef)
                               return Address;
   function GetPointerToFunctionOrStub (EE : ExecutionEngineRef;
                                        Func : ValueRef)
                                       return Address;

private
   pragma Import (C, LinkInJIT, "LLVMLinkInJIT");
   pragma Import (C, LinkInMCJIT, "LLVMLinkInMCJIT");
   pragma Import (C, LinkInInterpreter, "LLVMLinkInInterpreter");

   pragma Import (C, CreateGenericValueOfInt, "LLVMCreateGenericValueOfInt");
   pragma Import (C, CreateGenericValueOfPointer,
                  "LLVMCreateGenericValueOfPointer");
   pragma Import (C, CreateGenericValueOfFloat,
                  "LLVMCreateGenericValueOfFloat");
   pragma Import (C, GenericValueIntWidth, "LLVMGenericValueIntWidth");
   pragma Import (C, GenericValueToInt, "LLVMGenericValueToInt");
   pragma Import (C, GenericValueToPointer, "LLVMGenericValueToPointer");
   pragma Import (C, GenericValueToFloat, "LLVMGenericValueToFloat");
   pragma Import (C, DisposeGenericValue, "LLVMDisposeGenericValue");

   -- Operations on execution engines -----------------------------------

   pragma Import (C, CreateExecutionEngineForModule,
                  "LLVMCreateExecutionEngineForModule");
   pragma Import (C, CreateInterpreterForModule,
                  "LLVMCreateInterpreterForModule");
   pragma Import (C, CreateJITCompilerForModule,
                  "LLVMCreateJITCompilerForModule");
   pragma Import (C, DisposeExecutionEngine, "LLVMDisposeExecutionEngine");
   pragma Import (C, RunStaticConstructors, "LLVMRunStaticConstructors");
   pragma Import (C, RunStaticDestructors, "LLVMRunStaticDestructors");
   pragma Import (C, RunFunctionAsMain, "LLVMRunFunctionAsMain");
   pragma Import (C, RunFunction, "LLVMRunFunction");
   pragma Import (C, FreeMachineCodeForFunction,
                  "LLVMFreeMachineCodeForFunction");
   pragma Import (C, AddModule, "LLVMAddModule");
   pragma Import (C, RemoveModule, "LLVMRemoveModule");
   pragma Import (C, FindFunction, "LLVMFindFunction");
   pragma Import (C, GetExecutionEngineTargetData,
                "LLVMGetExecutionEngineTargetData");
   pragma Import (C, AddGlobalMapping, "LLVMAddGlobalMapping");

   pragma Import (C, GetPointerToFunctionOrStub,
                  "LLVMGetPointerToFunctionOrStub");
   pragma Import (C, GetPointerToGlobal,
                  "LLVMGetPointerToGlobal");
end LLVM.ExecutionEngine;
"p">[x, y] += delay_map_count[x+p, y+q] delay_map_sum += 0.1 * neigh_sum delay_map_sum2 += 0.1 * neigh_sum2 delay_map_count += 0.1 * neigh_count delay_map = delay_map_sum / delay_map_count delay_map_std = np.sqrt(delay_map_count*delay_map_sum2 - delay_map_sum**2) / delay_map_count #%% Print src-dst-pair summary print("Src-Dst-Type pair summary:") for cnt, src, dst in sorted([(v, k[0], k[1]) for k, v in src_dst_pairs.items()]): print("%20s %20s %5d%s" % (src, dst, cnt, " *" if src == sel_src_type and dst == sel_dst_type else "")) print() #%% Plot estimate vs actual delay plt.figure(figsize=(8, 3)) plt.title("Estimate vs Actual Delay") plt.plot(all_delay_data[:, 0], all_delay_data[:, 1], ".") plt.plot(delay_data[:, 0], delay_data[:, 1], ".") plt.plot([0, max_delay], [0, max_delay], "k") plt.ylabel("Estimated Delay") plt.xlabel("Actual Delay") plt.grid() plt.show() #%% Plot delay heatmap and std dev heatmap plt.figure(figsize=(9, 3)) plt.subplot(121) plt.title("Actual Delay Map") plt.imshow(delay_map) plt.colorbar() plt.subplot(122) plt.title("Standard Deviation") plt.imshow(delay_map_std) plt.colorbar() plt.show() #%% Generate Model #0 def nonlinearPreprocessor0(dx, dy): dx, dy = abs(dx), abs(dy) values = [1.0] values.append(dx + dy) return np.array(values) A = np.zeros((41*41, len(nonlinearPreprocessor0(0, 0)))) b = np.zeros(41*41) index = 0 for x in range(41): for y in range(41): if delay_map_count[x, y] > 0: A[index, :] = nonlinearPreprocessor0(x-20, y-20) b[index] = delay_map[x, y] index += 1 model0_params, _, _, _ = np.linalg.lstsq(A, b) print("Model #0 parameters:", model0_params) model0_map = np.zeros((41, 41)) for x in range(41): for y in range(41): v = np.dot(model0_params, nonlinearPreprocessor0(x-20, y-20)) model0_map[x, y] = v plt.figure(figsize=(9, 3)) plt.subplot(121) plt.title("Model #0 Delay Map") plt.imshow(model0_map) plt.colorbar() plt.subplot(122) plt.title("Model #0 Error Map") plt.imshow(model0_map - delay_map) plt.colorbar() plt.show() for i in range(delay_data.shape[0]): dx = delay_data[i, 2] dy = delay_data[i, 3] delay_data[i, 4] = np.dot(model0_params, nonlinearPreprocessor0(dx, dy)) plt.figure(figsize=(8, 3)) plt.title("Model #0 vs Actual Delay") plt.plot(delay_data[:, 0], delay_data[:, 4], ".") plt.plot(delay_map.flat, model0_map.flat, ".") plt.plot([0, max_delay], [0, max_delay], "k") plt.ylabel("Model #0 Delay") plt.xlabel("Actual Delay") plt.grid() plt.show() print("In-sample RMS error: %f" % np.sqrt(np.nanmean((delay_map - model0_map)**2))) print("Out-of-sample RMS error: %f" % np.sqrt(np.nanmean((delay_data[:, 0] - delay_data[:, 4])**2))) print() #%% Generate Model #1 def nonlinearPreprocessor1(dx, dy): dx, dy = abs(dx), abs(dy) values = [1.0] values.append(dx + dy) # 1-norm values.append((dx**2 + dy**2)**(1/2)) # 2-norm values.append((dx**3 + dy**3)**(1/3)) # 3-norm return np.array(values) A = np.zeros((41*41, len(nonlinearPreprocessor1(0, 0)))) b = np.zeros(41*41) index = 0 for x in range(41): for y in range(41): if delay_map_count[x, y] > 0: A[index, :] = nonlinearPreprocessor1(x-20, y-20) b[index] = delay_map[x, y] index += 1 model1_params, _, _, _ = np.linalg.lstsq(A, b) print("Model #1 parameters:", model1_params) model1_map = np.zeros((41, 41)) for x in range(41): for y in range(41): v = np.dot(model1_params, nonlinearPreprocessor1(x-20, y-20)) model1_map[x, y] = v plt.figure(figsize=(9, 3)) plt.subplot(121) plt.title("Model #1 Delay Map") plt.imshow(model1_map) plt.colorbar() plt.subplot(122) plt.title("Model #1 Error Map") plt.imshow(model1_map - delay_map) plt.colorbar() plt.show() for i in range(delay_data.shape[0]): dx = delay_data[i, 2] dy = delay_data[i, 3] delay_data[i, 5] = np.dot(model1_params, nonlinearPreprocessor1(dx, dy)) plt.figure(figsize=(8, 3)) plt.title("Model #1 vs Actual Delay") plt.plot(delay_data[:, 0], delay_data[:, 5], ".") plt.plot(delay_map.flat, model1_map.flat, ".") plt.plot([0, max_delay], [0, max_delay], "k") plt.ylabel("Model #1 Delay") plt.xlabel("Actual Delay") plt.grid() plt.show() print("In-sample RMS error: %f" % np.sqrt(np.nanmean((delay_map - model1_map)**2))) print("Out-of-sample RMS error: %f" % np.sqrt(np.nanmean((delay_data[:, 0] - delay_data[:, 5])**2))) print() #%% Generate Model #2 def nonlinearPreprocessor2(v): return np.array([1, v, np.sqrt(v)]) A = np.zeros((41*41, len(nonlinearPreprocessor2(0)))) b = np.zeros(41*41) index = 0 for x in range(41): for y in range(41): if delay_map_count[x, y] > 0: A[index, :] = nonlinearPreprocessor2(model1_map[x, y]) b[index] = delay_map[x, y] index += 1 model2_params, _, _, _ = np.linalg.lstsq(A, b) print("Model #2 parameters:", model2_params) model2_map = np.zeros((41, 41)) for x in range(41): for y in range(41): v = np.dot(model1_params, nonlinearPreprocessor1(x-20, y-20)) v = np.dot(model2_params, nonlinearPreprocessor2(v)) model2_map[x, y] = v plt.figure(figsize=(9, 3)) plt.subplot(121) plt.title("Model #2 Delay Map") plt.imshow(model2_map) plt.colorbar() plt.subplot(122) plt.title("Model #2 Error Map") plt.imshow(model2_map - delay_map) plt.colorbar() plt.show() for i in range(delay_data.shape[0]): dx = delay_data[i, 2] dy = delay_data[i, 3] delay_data[i, 6] = np.dot(model2_params, nonlinearPreprocessor2(delay_data[i, 5])) plt.figure(figsize=(8, 3)) plt.title("Model #2 vs Actual Delay") plt.plot(delay_data[:, 0], delay_data[:, 6], ".") plt.plot(delay_map.flat, model2_map.flat, ".") plt.plot([0, max_delay], [0, max_delay], "k") plt.ylabel("Model #2 Delay") plt.xlabel("Actual Delay") plt.grid() plt.show() print("In-sample RMS error: %f" % np.sqrt(np.nanmean((delay_map - model2_map)**2))) print("Out-of-sample RMS error: %f" % np.sqrt(np.nanmean((delay_data[:, 0] - delay_data[:, 6])**2))) print() #%% Generate deltas for different source net types type_deltas = dict() print("Delay deltas for different src types:") for src_type in sorted(type_delta_data.keys()): deltas = list() for dx, dy, delay in type_delta_data[src_type]: dx = abs(dx) dy = abs(dy) if dx > 1 or dy > 1: est = model0_params[0] + model0_params[1] * (dx + dy) else: est = mean_neighbour_tile_delays deltas.append(delay - est) print("%15s: %8.2f (std %6.2f)" % (\ src_type, np.mean(deltas), np.std(deltas))) type_deltas[src_type] = np.mean(deltas) #%% Print C defs of model parameters print("--snip--") print("%d, %d, %d," % (mean_neighbour_tile_delays, 128 * model0_params[0], 128 * model0_params[1])) print("%d, %d, %d, %d," % (128 * model1_params[0], 128 * model1_params[1], 128 * model1_params[2], 128 * model1_params[3])) print("%d, %d, %d," % (128 * model2_params[0], 128 * model2_params[1], 128 * model2_params[2])) print("%d, %d, %d, %d" % (type_deltas["LOCAL"], type_deltas["LUTFF_IN"], \ (type_deltas["SP4_H"] + type_deltas["SP4_V"]) / 2, (type_deltas["SP12_H"] + type_deltas["SP12_V"]) / 2)) print("--snap--")