xref: /libCEED/interface/ceed-jit-tools.c (revision 947f93aa7135eb1759bf2866bd2fbd481436b113)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include <ceed/ceed.h>
9 #include <ceed/backend.h>
10 #include <ceed/jit-tools.h>
11 #include <ceed-impl.h>
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <string.h>
15 
16 /**
17   @brief Check if valid file exists at path given
18 
19   @param ceed                  A Ceed object for error handling
20   @param[in]  source_file_path Absolute path to source file
21   @param[out] is_valid         Boolean flag indicating if file can be opend
22 
23   @return An error code: 0 - success, otherwise - failure
24 
25   @ref Backend
26 **/
27 int CeedCheckFilePath(Ceed ceed, const char *source_file_path, bool *is_valid) {
28   int ierr;
29 
30   // Sometimes we have path/to/file.h:function_name
31   // Create tempory file path without name, if needed
32   char *source_file_path_only;
33   char *last_colon = strrchr(source_file_path, ':');
34   if (last_colon) {
35     size_t source_file_path_length = (last_colon - source_file_path + 1);
36 
37     ierr = CeedCalloc(source_file_path_length, &source_file_path_only);
38     CeedChk(ierr);
39     memcpy(source_file_path_only, source_file_path, source_file_path_length - 1);
40   } else {
41     source_file_path_only = (char *)source_file_path;
42   }
43 
44   // Debug
45   CeedDebug256(ceed, 1, "Checking for source file: ");
46   CeedDebug(ceed, "%s\n", source_file_path_only);
47 
48   // Check for valid file path
49   FILE *source_file;
50   source_file = fopen(source_file_path_only, "rb");
51   *is_valid = !!source_file;
52 
53   if (*is_valid) {
54     // Debug
55     CeedDebug256(ceed, 1, "Found JiT source file: ");
56     CeedDebug(ceed, "%s\n", source_file_path_only);
57 
58     fclose(source_file);
59   }
60 
61   // Free temp file path, if used
62   if (last_colon) {
63     ierr = CeedFree(&source_file_path_only); CeedChk(ierr);
64   }
65 
66   return CEED_ERROR_SUCCESS;
67 }
68 
69 /**
70   @brief Load source file into initialized string buffer, including full text
71            of local files in place of `#include "local.h"`
72 
73   @param ceed                  A Ceed object for error handling
74   @param[in]  source_file_path Absolute path to source file
75   @param[out] buffer           String buffer for source file contents
76 
77   @return An error code: 0 - success, otherwise - failure
78 
79   @ref Backend
80 **/
81 int CeedLoadSourceToInitializedBuffer(Ceed ceed,
82                                       const char *source_file_path, char **buffer) {
83   int ierr;
84   FILE *source_file;
85   long file_size, file_offset = 0;
86   char *temp_buffer;
87 
88   // Debug
89   CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n");
90   CeedDebug256(ceed, 1, "Current source file: ");
91   CeedDebug(ceed, "%s\n", source_file_path);
92   CeedDebug256(ceed, 1, "Current buffer:\n");
93   CeedDebug(ceed, "%s\n", *buffer);
94 
95   // Read file to temporary buffer
96   source_file = fopen(source_file_path, "rb");
97   if (!source_file)
98     // LCOV_EXCL_START
99     return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't open source file: %s",
100                      source_file_path);
101   // LCOV_EXCL_STOP
102   // -- Compute size of source
103   fseek(source_file, 0L, SEEK_END);
104   file_size = ftell(source_file);
105   rewind(source_file);
106   //  -- Allocate memory for entire source file
107   ierr = CeedCalloc(file_size + 1, &temp_buffer); CeedChk(ierr);
108   // -- Copy the file into the buffer
109   if (1 != fread(temp_buffer, file_size, 1, source_file)) {
110     // LCOV_EXCL_START
111     fclose(source_file);
112     ierr = CeedFree(&temp_buffer); CeedChk(ierr);
113     return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't read source file: %s",
114                      source_file_path);
115     // LCOV_EXCL_STOP
116   }
117   fclose(source_file);
118 
119   // Search for headers to include
120   const char *first_hash = strchr(temp_buffer, '#');
121   while (first_hash) {
122     // -- Check for 'include' keyword
123     const char *next_e = strchr(first_hash, 'e');
124     char keyword[8] = "";
125     if (next_e && next_e - first_hash >= 7) memcpy(keyword, &next_e[-6], 7);
126     bool is_hash_include = !strcmp(keyword, "include");
127     // ---- Spaces allowed in '#  include <header.h>'
128     if (next_e) {
129       for (CeedInt i = 1; first_hash - next_e + i < -6; i++) {
130         is_hash_include &= first_hash[i] == ' ';
131       }
132     }
133     if (is_hash_include) {
134       // -- Copy into buffer all preceding #
135       long current_size = strlen(*buffer);
136       long copy_size = first_hash - &temp_buffer[file_offset];
137       ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr);
138       memcpy(&(*buffer)[current_size], "\n", 2);
139       memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
140       memcpy(&(*buffer)[current_size + copy_size], "", 1);
141       // -- Load local "header.h"
142       char *next_quote = strchr(first_hash, '"');
143       char *next_new_line = strchr(first_hash, '\n');
144       bool is_local_header = is_hash_include && next_quote &&
145                              (next_new_line - next_quote > 0);
146       char *next_left_chevron = strchr(first_hash, '<');
147       bool is_ceed_header = is_hash_include && next_left_chevron &&
148                             (next_new_line - next_left_chevron > 0) &&
149                             (!strncmp(next_left_chevron, "<ceed/jit-source/", 17) ||
150                              !strncmp(next_left_chevron, "<ceed/types.h>", 14) ||
151                              !strncmp(next_left_chevron, "<ceed/ceed-f32.h>", 17) ||
152                              !strncmp(next_left_chevron, "<ceed/ceed-f64.h>", 17));
153       if (is_local_header || is_ceed_header) {
154         // ---- Build source path
155         char *include_source_path;
156         if (is_local_header) {
157           long root_length = strrchr(source_file_path, '/') - source_file_path;
158           long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1;
159           ierr = CeedCalloc(root_length + include_file_name_len + 2,
160                             &include_source_path); CeedChk(ierr);
161           memcpy(include_source_path, source_file_path, root_length + 1);
162           memcpy(&include_source_path[root_length + 1], &next_quote[1],
163                  include_file_name_len);
164           memcpy(&include_source_path[root_length + include_file_name_len + 1], "", 1);
165         } else {
166           char *next_right_chevron = strchr(first_hash, '>');
167           char *ceed_relative_path;
168           long ceed_relative_path_length = next_right_chevron - next_left_chevron - 1;
169           ierr = CeedCalloc(ceed_relative_path_length + 1, &ceed_relative_path);
170           CeedChk(ierr);
171           memcpy(ceed_relative_path, &next_left_chevron[1], ceed_relative_path_length);
172           ierr = CeedGetJitAbsolutePath(ceed, ceed_relative_path, &include_source_path);
173           CeedChk(ierr);
174           ierr = CeedFree(&ceed_relative_path); CeedChk(ierr);
175         }
176         // ---- Recursive call to load source to buffer
177         CeedDebug256(ceed, 2, "JiT Including: %s\n", include_source_path);
178         CeedChk(ierr);
179         ierr = CeedLoadSourceToInitializedBuffer(ceed, include_source_path, buffer);
180         CeedChk(ierr);
181         ierr = CeedFree(&include_source_path); CeedChk(ierr);
182       }
183       file_offset = strchr(first_hash, '\n') - temp_buffer + 1;
184     }
185     // -- Next hash
186     first_hash = strchr(&first_hash[1], '#');
187   }
188   // Copy rest of source file into buffer
189   long current_size = strlen(*buffer);
190   long copy_size = strlen(&temp_buffer[file_offset]);
191   ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr);
192   memcpy(&(*buffer)[current_size], "\n", 2);
193   memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
194   memcpy(&(*buffer)[current_size + copy_size + 1], "", 1);
195 
196   // Cleanup
197   ierr = CeedFree(&temp_buffer); CeedChk(ierr);
198 
199   // Debug
200   CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n");
201   CeedDebug256(ceed, 1, "Current source file: ");
202   CeedDebug(ceed, "%s\n", source_file_path);
203   CeedDebug256(ceed, 1, "Final buffer:\n");
204   CeedDebug(ceed, "%s\n", *buffer);
205 
206   return CEED_ERROR_SUCCESS;
207 }
208 
209 /**
210   @brief Initalize and load source file into string buffer, including full text
211            of local files in place of `#include "local.h"`.
212          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
213 
214   @param ceed                  A Ceed object for error handling
215   @param[in]  source_file_path Absolute path to source file
216   @param[out] buffer           String buffer for source file contents
217 
218   @return An error code: 0 - success, otherwise - failure
219 
220   @ref Backend
221 **/
222 int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path,
223                            char **buffer) {
224   int ierr;
225 
226   // Initalize buffer
227   ierr = CeedCalloc(1, buffer); CeedChk(ierr);
228 
229   // Load to initalized buffer
230   ierr = CeedLoadSourceToInitializedBuffer(ceed, source_file_path, buffer);
231   CeedChk(ierr);
232 
233   return CEED_ERROR_SUCCESS;
234 }
235 
236 /**
237   @brief Build an absolute filepath from a base filepath and an absolute filepath.
238            This helps construct source file paths for `CeedLoadSourceToBuffer()`.
239          Note: Caller is responsible for freeing the string buffer with `CeedFree()`.
240 
241   @param ceed                     A Ceed object for error handling
242   @param[in]  base_file_path      Absolute path to current file
243   @param[in]  relative_file_path  Relative path to target file
244   @param[out] new_file_path       String buffer for absolute path to target file
245 
246   @return An error code: 0 - success, otherwise - failure
247 
248   @ref Backend
249 **/
250 int CeedPathConcatenate(Ceed ceed, const char *base_file_path,
251                         const char *relative_file_path, char **new_file_path) {
252   int ierr;
253   char *last_slash = strrchr(base_file_path, '/');
254   size_t base_length = (last_slash - base_file_path + 1),
255          relative_length = strlen(relative_file_path),
256          new_file_path_length = base_length + relative_length + 1;
257 
258   ierr = CeedCalloc(new_file_path_length, new_file_path); CeedChk(ierr);
259   memcpy(*new_file_path, base_file_path, base_length);
260   memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length);
261 
262   return CEED_ERROR_SUCCESS;
263 }
264 
265 /**
266   @brief Find the relative filepath to an installed JiT file
267 
268   @param[in]  absolute_file_path Absolute path to installed JiT file
269   @param[out] relative_file_path Relative path to installed JiT file
270 
271   @return An error code: 0 - success, otherwise - failure
272 
273   @ref Backend
274 **/
275 int CeedGetJitRelativePath(const char *absolute_file_path,
276                            const char **relative_file_path) {
277   *(relative_file_path) = strstr(absolute_file_path, "ceed/jit-source");
278 
279   if (!*relative_file_path)
280     // LCOV_EXCL_START
281     return CeedError(NULL, CEED_ERROR_MAJOR,
282                      "Couldn't find relative path including "
283                      "'ceed/jit-source' for: %s", absolute_file_path);
284   // LCOV_EXCL_STOP
285 
286   return CEED_ERROR_SUCCESS;
287 }
288 
289 /**
290   @brief Build an absolute filepath to a JiT file
291 
292   @param ceed                    A Ceed object for error handling
293   @param[in]  relative_file_path Relative path to installed JiT file
294   @param[out] absolute_file_path String buffer for absolute path to target file
295 
296   @return An error code: 0 - success, otherwise - failure
297 
298   @ref Backend
299 **/
300 int CeedGetJitAbsolutePath(Ceed ceed, const char *relative_file_path,
301                            char **absolute_file_path) {
302   int ierr;
303   Ceed ceed_parent;
304 
305   // Debug
306   CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n");
307   CeedDebug256(ceed, 1, "Relative JiT source file: ");
308   CeedDebug(ceed, "%s\n", relative_file_path);
309 
310 
311   ierr = CeedGetParent(ceed, &ceed_parent); CeedChk(ierr);
312   for (CeedInt i = 0; i < ceed_parent->num_jit_source_roots; i++) {
313     bool is_valid;
314 
315     // Debug
316     CeedDebug256(ceed, 1, "Checking JiT root: ");
317     CeedDebug(ceed, "%s\n", ceed_parent->jit_source_roots[i]);
318 
319     // Build and check absolute path with current root
320     ierr = CeedPathConcatenate(ceed, ceed_parent->jit_source_roots[i],
321                                relative_file_path, absolute_file_path);
322     CeedChk(ierr);
323     ierr = CeedCheckFilePath(ceed, *absolute_file_path, &is_valid); CeedChk(ierr);
324 
325     if (is_valid) {
326       return CEED_ERROR_SUCCESS;
327     } else {
328       ierr = CeedFree(absolute_file_path); CeedChk(ierr);
329     }
330   }
331 
332   // LCOV_EXCL_START
333   return CeedError(ceed, CEED_ERROR_MAJOR,
334                    "Couldn't find matching JiT source file: %s",
335                    relative_file_path);
336   // LCOV_EXCL_STOP
337 }
338