xref: /libCEED/interface/ceed-jit-tools.c (revision 9c25dd66b9687765a7022cc762ccaf201b721845)
1 // Copyright (c) 2017-2024, 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-impl.h>
9 #include <ceed.h>
10 #include <ceed/backend.h>
11 #include <ceed/jit-tools.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[in]  ceed             `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 opened
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   // Sometimes we have path/to/file.h:function_name
29   // Create temporary file path without name, if needed
30   char *source_file_path_only;
31   char *last_colon = strrchr(source_file_path, ':');
32 
33   if (last_colon) {
34     size_t source_file_path_length = (last_colon - source_file_path + 1);
35 
36     CeedCall(CeedCalloc(source_file_path_length, &source_file_path_only));
37     memcpy(source_file_path_only, source_file_path, source_file_path_length - 1);
38   } else {
39     source_file_path_only = (char *)source_file_path;
40   }
41 
42   // Debug
43   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Checking for source file: ");
44   CeedDebug(ceed, "%s\n", source_file_path_only);
45 
46   // Check for valid file path
47   FILE *source_file;
48   source_file = fopen(source_file_path_only, "rb");
49   *is_valid   = source_file;
50 
51   if (*is_valid) {
52     // Debug
53     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Found JiT source file: ");
54     CeedDebug(ceed, "%s\n", source_file_path_only);
55     fclose(source_file);
56   }
57 
58   // Free temp file path, if used
59   if (last_colon) CeedCall(CeedFree(&source_file_path_only));
60   return CEED_ERROR_SUCCESS;
61 }
62 
63 /**
64   @brief Normalize a filepath
65 
66   @param[in]   ceed                        `Ceed` object for error handling
67   @param[in]   source_file_path            Absolute path to source file
68   @param[out]  normalized_source_file_path Normalized filepath
69 
70   @return An error code: 0 - success, otherwise - failure
71 
72   @ref Backend
73 **/
74 static int CeedNormalizePath(Ceed ceed, const char *source_file_path, char **normalized_source_file_path) {
75   CeedCall(CeedStringAllocCopy(source_file_path, normalized_source_file_path));
76 
77   char *first_dot = strchr(*normalized_source_file_path, '.');
78 
79   while (first_dot) {
80     char *search_from = first_dot + 1;
81     char  keyword[5]  = "";
82 
83     // -- Check for /./ and covert to /
84     if (first_dot != *normalized_source_file_path && strlen(first_dot) > 2) memcpy(keyword, &first_dot[-1], 3);
85     bool is_here = !strcmp(keyword, "/./");
86 
87     if (is_here) {
88       for (CeedInt i = 0; first_dot[i - 1]; i++) first_dot[i] = first_dot[i + 2];
89       search_from = first_dot;
90     } else {
91       // -- Check for /foo/../ and convert to /
92       if (first_dot != *normalized_source_file_path && strlen(first_dot) > 3) memcpy(keyword, &first_dot[-1], 4);
93       bool is_up_one = !strcmp(keyword, "/../");
94 
95       if (is_up_one) {
96         char *last_slash = &first_dot[-2];
97 
98         while (last_slash[0] != '/' && last_slash != *normalized_source_file_path) last_slash--;
99         CeedCheck(last_slash != *normalized_source_file_path, ceed, CEED_ERROR_MAJOR, "Malformed source path %s", source_file_path);
100         for (CeedInt i = 0; first_dot[i + 1]; i++) last_slash[i] = first_dot[i + 2];
101         search_from = last_slash;
102       }
103     }
104     first_dot = strchr(search_from, '.');
105   }
106   return CEED_ERROR_SUCCESS;
107 }
108 
109 /**
110   @brief Load source file into initialized string buffer, including full text of local files in place of `#include "local.h"`.
111     This also updates the `num_file_paths` and `source_file_paths`.
112     Callers are responsible freeing all filepath strings and the string buffer with @ref CeedFree().
113 
114   @param[in]     ceed             `Ceed` object for error handling
115   @param[in]     source_file_path Absolute path to source file
116   @param[in,out] num_file_paths   Number of files already included
117   @param[in,out] file_paths       Paths of files already included
118   @param[out]    buffer           String buffer for source file contents
119 
120   @return An error code: 0 - success, otherwise - failure
121 
122   @ref Backend
123 **/
124 int CeedLoadSourceToInitializedBuffer(Ceed ceed, const char *source_file_path, CeedInt *num_file_paths, char ***file_paths, char **buffer) {
125   FILE *source_file;
126   long  file_size, file_offset = 0;
127   char *temp_buffer;
128 
129   // Debug
130   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n");
131   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current source file: ");
132   CeedDebug(ceed, "%s\n", source_file_path);
133   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current buffer:\n");
134   CeedDebug(ceed, "%s\n", *buffer);
135 
136   // Read file to temporary buffer
137   source_file = fopen(source_file_path, "rb");
138   CeedCheck(source_file, ceed, CEED_ERROR_MAJOR, "Couldn't open source file: %s", source_file_path);
139   // -- Compute size of source
140   fseek(source_file, 0L, SEEK_END);
141   file_size = ftell(source_file);
142   rewind(source_file);
143   //  -- Allocate memory for entire source file
144   CeedCall(CeedCalloc(file_size + 1, &temp_buffer));
145   // -- Copy the file into the buffer
146   if (1 != fread(temp_buffer, file_size, 1, source_file)) {
147     // LCOV_EXCL_START
148     fclose(source_file);
149     CeedCall(CeedFree(&temp_buffer));
150     return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't read source file: %s", source_file_path);
151     // LCOV_EXCL_STOP
152   }
153   fclose(source_file);
154 
155   // Search for headers to include
156   const char *first_hash = strchr(temp_buffer, '#');
157 
158   while (first_hash) {
159     // -- Check for 'pragma' keyword
160     const char *next_m     = strchr(first_hash, 'm');
161     char        keyword[8] = "";
162 
163     if (next_m && next_m - first_hash >= 5) memcpy(keyword, &next_m[-4], 6);
164     bool is_hash_pragma = !strcmp(keyword, "pragma");
165 
166     // ---- Spaces allowed in '#  pragma'
167     if (next_m) {
168       for (CeedInt i = 1; first_hash - next_m + i < -5; i++) {
169         is_hash_pragma &= first_hash[i] == ' ';
170       }
171     }
172     if (is_hash_pragma) {
173       // -- Check if '#pragma once'
174       char *next_o         = strchr(first_hash, 'o');
175       char *next_new_line  = strchr(first_hash, '\n');
176       bool  is_pragma_once = next_o && (next_new_line - next_o > 0) && !strncmp(next_o, "once", 4);
177 
178       // -- Copy into buffer, omitting last line if #pragma once
179       long current_size = strlen(*buffer);
180       long copy_size    = first_hash - &temp_buffer[file_offset] + (is_pragma_once ? 0 : (next_new_line - first_hash + 1));
181 
182       CeedCall(CeedRealloc(current_size + copy_size + 2, buffer));
183       memcpy(&(*buffer)[current_size], "\n", 2);
184       memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
185       memcpy(&(*buffer)[current_size + copy_size], "", 1);
186 
187       file_offset = strchr(first_hash, '\n') - temp_buffer + 1;
188     }
189 
190     // -- Check for 'include' keyword
191     const char *next_e = strchr(first_hash, 'e');
192 
193     if (next_e && next_e - first_hash >= 7) memcpy(keyword, &next_e[-6], 7);
194     bool is_hash_include = !strcmp(keyword, "include");
195 
196     // ---- Spaces allowed in '#  include <header.h>'
197     if (next_e) {
198       for (CeedInt i = 1; first_hash - next_e + i < -6; i++) {
199         is_hash_include &= first_hash[i] == ' ';
200       }
201     }
202     if (is_hash_include) {
203       // -- Copy into buffer all preceding #
204       long current_size = strlen(*buffer);
205       long copy_size    = first_hash - &temp_buffer[file_offset];
206 
207       CeedCall(CeedRealloc(current_size + copy_size + 2, buffer));
208       memcpy(&(*buffer)[current_size], "\n", 2);
209       memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
210       memcpy(&(*buffer)[current_size + copy_size], "", 1);
211       // -- Load local "header.h"
212       char *next_quote        = strchr(first_hash, '"');
213       char *next_new_line     = strchr(first_hash, '\n');
214       bool  is_local_header   = is_hash_include && next_quote && (next_new_line - next_quote > 0);
215       char *next_left_chevron = strchr(first_hash, '<');
216       bool  is_ceed_header    = next_left_chevron && (next_new_line - next_left_chevron > 0) &&
217                             (!strncmp(next_left_chevron, "<ceed/jit-source/", 17) || !strncmp(next_left_chevron, "<ceed/types.h>", 14) ||
218                              !strncmp(next_left_chevron, "<ceed/ceed-f32.h>", 17) || !strncmp(next_left_chevron, "<ceed/ceed-f64.h>", 17));
219       bool is_std_header =
220           next_left_chevron && (next_new_line - next_left_chevron > 0) &&
221           (!strncmp(next_left_chevron, "<std", 4) || !strncmp(next_left_chevron, "<math.h>", 8) || !strncmp(next_left_chevron, "<ceed", 5));
222 
223       if (is_local_header || is_ceed_header) {
224         // ---- Build source path
225         bool  is_included = false;
226         char *include_source_path;
227 
228         if (is_local_header) {
229           long root_length           = strrchr(source_file_path, '/') - source_file_path;
230           long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1;
231 
232           CeedCall(CeedCalloc(root_length + include_file_name_len + 2, &include_source_path));
233           memcpy(include_source_path, source_file_path, root_length + 1);
234           memcpy(&include_source_path[root_length + 1], &next_quote[1], include_file_name_len);
235           memcpy(&include_source_path[root_length + include_file_name_len + 1], "", 1);
236         } else {
237           char *next_right_chevron = strchr(first_hash, '>');
238           char *ceed_relative_path;
239           long  ceed_relative_path_length = next_right_chevron - next_left_chevron - 1;
240 
241           CeedCall(CeedCalloc(ceed_relative_path_length + 1, &ceed_relative_path));
242           memcpy(ceed_relative_path, &next_left_chevron[1], ceed_relative_path_length);
243           CeedCall(CeedGetJitAbsolutePath(ceed, ceed_relative_path, (const char **)&include_source_path));
244           CeedCall(CeedFree(&ceed_relative_path));
245         }
246         // ---- Recursive call to load source to buffer
247         char *normalized_include_source_path;
248 
249         CeedCall(CeedNormalizePath(ceed, include_source_path, &normalized_include_source_path));
250         for (CeedInt i = 0; i < *num_file_paths; i++) is_included |= !strcmp(normalized_include_source_path, (*file_paths)[i]);
251         if (!is_included) {
252           CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "JiT Including: %s\n", normalized_include_source_path);
253           CeedCall(CeedLoadSourceToInitializedBuffer(ceed, normalized_include_source_path, num_file_paths, file_paths, buffer));
254           CeedCall(CeedRealloc(*num_file_paths + 1, file_paths));
255           CeedCall(CeedStringAllocCopy(normalized_include_source_path, &(*file_paths)[*num_file_paths]));
256           (*num_file_paths)++;
257         }
258         CeedCall(CeedFree(&include_source_path));
259         CeedCall(CeedFree(&normalized_include_source_path));
260       } else if (!is_std_header) {
261         long header_copy_size = next_new_line - first_hash + 1;
262 
263         CeedCall(CeedRealloc(current_size + copy_size + header_copy_size + 2, buffer));
264         memcpy(&(*buffer)[current_size + copy_size], "\n", 2);
265         memcpy(&(*buffer)[current_size + copy_size + 1], first_hash, header_copy_size);
266         memcpy(&(*buffer)[current_size + copy_size + header_copy_size], "", 1);
267       }
268       file_offset = strchr(first_hash, '\n') - temp_buffer + 1;
269     }
270     // -- Next hash
271     first_hash = strchr(&first_hash[1], '#');
272   }
273   // Copy rest of source file into buffer
274   long current_size = strlen(*buffer);
275   long copy_size    = strlen(&temp_buffer[file_offset]);
276 
277   CeedCall(CeedRealloc(current_size + copy_size + 2, buffer));
278   memcpy(&(*buffer)[current_size], "\n", 2);
279   memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
280   memcpy(&(*buffer)[current_size + copy_size + 1], "", 1);
281 
282   // Cleanup
283   CeedCall(CeedFree(&temp_buffer));
284 
285   // Debug
286   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n");
287   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current source file: ");
288   CeedDebug(ceed, "%s\n", source_file_path);
289   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Final buffer:\n");
290   CeedDebug(ceed, "%s\n", *buffer);
291   return CEED_ERROR_SUCCESS;
292 }
293 
294 /**
295   @brief Load source file into initialized string buffer, including full text of local files in place of `#include "local.h"`.
296     This also initializes and populates the `num_file_paths` and `source_file_paths`.
297     Callers are responsible freeing all filepath strings and the string buffer with @ref CeedFree().
298 
299   @param[in]     ceed             `Ceed` object for error handling
300   @param[in]     source_file_path Absolute path to source file
301   @param[in,out] num_file_paths   Number of files already included
302   @param[in,out] file_paths       Paths of files already included
303   @param[out]    buffer           String buffer for source file contents
304 
305   @return An error code: 0 - success, otherwise - failure
306 
307   @ref Backend
308 **/
309 int CeedLoadSourceAndInitializeBuffer(Ceed ceed, const char *source_file_path, CeedInt *num_file_paths, char ***file_paths, char **buffer) {
310   // Ensure defaults were set
311   *num_file_paths = 0;
312   *file_paths     = NULL;
313 
314   // Initialize
315   CeedCall(CeedCalloc(1, buffer));
316 
317   // And load source
318   CeedCall(CeedLoadSourceToInitializedBuffer(ceed, source_file_path, num_file_paths, file_paths, buffer));
319   return CEED_ERROR_SUCCESS;
320 }
321 
322 /**
323   @brief Initialize and load source file into string buffer, including full text of local files in place of `#include "local.h"`.
324     User @ref CeedLoadSourceAndInitializeBuffer() and @ref CeedLoadSourceToInitializedBuffer() if loading multiple source files into the same buffer.
325     Caller is responsible for freeing the string buffer with @ref CeedFree().
326 
327   @param[in]  ceed             `Ceed` object for error handling
328   @param[in]  source_file_path Absolute path to source file
329   @param[out] buffer           String buffer for source file contents
330 
331   @return An error code: 0 - success, otherwise - failure
332 
333   @ref Backend
334 **/
335 int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path, char **buffer) {
336   char  **file_paths     = NULL;
337   CeedInt num_file_paths = 0;
338 
339   // Load
340   CeedCall(CeedLoadSourceAndInitializeBuffer(ceed, source_file_path, &num_file_paths, &file_paths, buffer));
341 
342   // Cleanup
343   for (CeedInt i = 0; i < num_file_paths; i++) CeedCall(CeedFree(&file_paths[i]));
344   CeedCall(CeedFree(&file_paths));
345   return CEED_ERROR_SUCCESS;
346 }
347 
348 /**
349   @brief Build an absolute filepath from a base filepath and an absolute filepath.
350 
351   This helps construct source file paths for @ref CeedLoadSourceToBuffer().
352 
353   Note: Caller is responsible for freeing the string buffer with @ref CeedFree().
354 
355   @param[in]  ceed               `Ceed` object for error handling
356   @param[in]  base_file_path     Absolute path to current file
357   @param[in]  relative_file_path Relative path to target file
358   @param[out] new_file_path      String buffer for absolute path to target file
359 
360   @return An error code: 0 - success, otherwise - failure
361 
362   @ref Backend
363 **/
364 int CeedPathConcatenate(Ceed ceed, const char *base_file_path, const char *relative_file_path, char **new_file_path) {
365   char  *last_slash  = strrchr(base_file_path, '/');
366   size_t base_length = (last_slash - base_file_path + 1), relative_length = strlen(relative_file_path),
367          new_file_path_length = base_length + relative_length + 1;
368 
369   CeedCall(CeedCalloc(new_file_path_length, new_file_path));
370   memcpy(*new_file_path, base_file_path, base_length);
371   memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length);
372   return CEED_ERROR_SUCCESS;
373 }
374 
375 /**
376   @brief Find the relative filepath to an installed JiT file
377 
378   @param[in]  absolute_file_path Absolute path to installed JiT file
379   @param[out] relative_file_path Relative path to installed JiT file, a substring of the absolute path
380 
381   @return An error code: 0 - success, otherwise - failure
382 
383   @ref Backend
384 **/
385 int CeedGetJitRelativePath(const char *absolute_file_path, const char **relative_file_path) {
386   *(relative_file_path) = strstr(absolute_file_path, "ceed/jit-source");
387   CeedCheck(*relative_file_path, NULL, CEED_ERROR_MAJOR, "Couldn't find relative path including 'ceed/jit-source' for: %s", absolute_file_path);
388   return CEED_ERROR_SUCCESS;
389 }
390 
391 /**
392   @brief Build an absolute filepath to a JiT file
393 
394   @param[in]  ceed               `Ceed` object for error handling
395   @param[in]  relative_file_path Relative path to installed JiT file
396   @param[out] absolute_file_path String buffer for absolute path to target file, to be freed by caller
397 
398   @return An error code: 0 - success, otherwise - failure
399 
400   @ref Backend
401 **/
402 int CeedGetJitAbsolutePath(Ceed ceed, const char *relative_file_path, const char **absolute_file_path) {
403   const char **jit_source_dirs;
404   CeedInt      num_source_dirs;
405 
406   // Debug
407   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n");
408   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Relative JiT source file: ");
409   CeedDebug(ceed, "%s\n", relative_file_path);
410 
411   CeedCallBackend(CeedGetJitSourceRoots(ceed, &num_source_dirs, &jit_source_dirs));
412   for (CeedInt i = 0; i < num_source_dirs; i++) {
413     bool is_valid;
414 
415     // Debug
416     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Checking JiT root: ");
417     CeedDebug(ceed, "%s\n", jit_source_dirs[i]);
418 
419     // Build and check absolute path with current root
420     CeedCall(CeedPathConcatenate(ceed, jit_source_dirs[i], relative_file_path, (char **)absolute_file_path));
421     CeedCall(CeedCheckFilePath(ceed, *absolute_file_path, &is_valid));
422 
423     if (is_valid) {
424       CeedCallBackend(CeedRestoreJitSourceRoots(ceed, &jit_source_dirs));
425       return CEED_ERROR_SUCCESS;
426     }
427     // LCOV_EXCL_START
428     else
429       CeedCall(CeedFree(absolute_file_path));
430     // LCOV_EXCL_STOP
431   }
432   // LCOV_EXCL_START
433   return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't find matching JiT source file: %s", relative_file_path);
434   // LCOV_EXCL_STOP
435 }
436