xref: /libCEED/interface/ceed-jit-tools.c (revision 9a25c3513918281076c2babe50808fbe5e3a546e)
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-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 Load source file into initialized string buffer, including full text of local files in place of `#include "local.h"`
65 
66   @param[in]  ceed             `Ceed` object for error handling
67   @param[in]  source_file_path Absolute path to source file
68   @param[out] buffer           String buffer for source file contents
69 
70   @return An error code: 0 - success, otherwise - failure
71 
72   @ref Backend
73 **/
74 int CeedLoadSourceToInitializedBuffer(Ceed ceed, const char *source_file_path, char **buffer) {
75   FILE *source_file;
76   long  file_size, file_offset = 0;
77   char *temp_buffer;
78 
79   // Debug
80   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n");
81   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current source file: ");
82   CeedDebug(ceed, "%s\n", source_file_path);
83   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current buffer:\n");
84   CeedDebug(ceed, "%s\n", *buffer);
85 
86   // Read file to temporary buffer
87   source_file = fopen(source_file_path, "rb");
88   CeedCheck(source_file, ceed, CEED_ERROR_MAJOR, "Couldn't open source file: %s", source_file_path);
89   // -- Compute size of source
90   fseek(source_file, 0L, SEEK_END);
91   file_size = ftell(source_file);
92   rewind(source_file);
93   //  -- Allocate memory for entire source file
94   CeedCall(CeedCalloc(file_size + 1, &temp_buffer));
95   // -- Copy the file into the buffer
96   if (1 != fread(temp_buffer, file_size, 1, source_file)) {
97     // LCOV_EXCL_START
98     fclose(source_file);
99     CeedCall(CeedFree(&temp_buffer));
100     return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't read source file: %s", source_file_path);
101     // LCOV_EXCL_STOP
102   }
103   fclose(source_file);
104 
105   // Search for headers to include
106   const char *first_hash = strchr(temp_buffer, '#');
107 
108   while (first_hash) {
109     // -- Check for 'include' keyword
110     const char *next_e     = strchr(first_hash, 'e');
111     char        keyword[8] = "";
112 
113     if (next_e && next_e - first_hash >= 7) memcpy(keyword, &next_e[-6], 7);
114     bool is_hash_include = !strcmp(keyword, "include");
115 
116     // ---- Spaces allowed in '#  include <header.h>'
117     if (next_e) {
118       for (CeedInt i = 1; first_hash - next_e + i < -6; i++) {
119         is_hash_include &= first_hash[i] == ' ';
120       }
121     }
122     if (is_hash_include) {
123       // -- Copy into buffer all preceding #
124       long current_size = strlen(*buffer);
125       long copy_size    = first_hash - &temp_buffer[file_offset];
126 
127       CeedCall(CeedRealloc(current_size + copy_size + 2, buffer));
128       memcpy(&(*buffer)[current_size], "\n", 2);
129       memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
130       memcpy(&(*buffer)[current_size + copy_size], "", 1);
131       // -- Load local "header.h"
132       char *next_quote        = strchr(first_hash, '"');
133       char *next_new_line     = strchr(first_hash, '\n');
134       bool  is_local_header   = is_hash_include && next_quote && (next_new_line - next_quote > 0);
135       char *next_left_chevron = strchr(first_hash, '<');
136       bool  is_ceed_header    = is_hash_include && next_left_chevron && (next_new_line - next_left_chevron > 0) &&
137                             (!strncmp(next_left_chevron, "<ceed/jit-source/", 17) || !strncmp(next_left_chevron, "<ceed/types.h>", 14) ||
138                              !strncmp(next_left_chevron, "<ceed/ceed-f32.h>", 17) || !strncmp(next_left_chevron, "<ceed/ceed-f64.h>", 17));
139 
140       if (is_local_header || is_ceed_header) {
141         // ---- Build source path
142         char *include_source_path;
143 
144         if (is_local_header) {
145           long root_length           = strrchr(source_file_path, '/') - source_file_path;
146           long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1;
147 
148           CeedCall(CeedCalloc(root_length + include_file_name_len + 2, &include_source_path));
149           memcpy(include_source_path, source_file_path, root_length + 1);
150           memcpy(&include_source_path[root_length + 1], &next_quote[1], include_file_name_len);
151           memcpy(&include_source_path[root_length + include_file_name_len + 1], "", 1);
152         } else {
153           char *next_right_chevron = strchr(first_hash, '>');
154           char *ceed_relative_path;
155           long  ceed_relative_path_length = next_right_chevron - next_left_chevron - 1;
156 
157           CeedCall(CeedCalloc(ceed_relative_path_length + 1, &ceed_relative_path));
158           memcpy(ceed_relative_path, &next_left_chevron[1], ceed_relative_path_length);
159           CeedCall(CeedGetJitAbsolutePath(ceed, ceed_relative_path, (const char **)&include_source_path));
160           CeedCall(CeedFree(&ceed_relative_path));
161         }
162         // ---- Recursive call to load source to buffer
163         CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "JiT Including: %s\n", include_source_path);
164         CeedCall(CeedLoadSourceToInitializedBuffer(ceed, include_source_path, buffer));
165         CeedCall(CeedFree(&include_source_path));
166       }
167       file_offset = strchr(first_hash, '\n') - temp_buffer + 1;
168     }
169     // -- Next hash
170     first_hash = strchr(&first_hash[1], '#');
171   }
172   // Copy rest of source file into buffer
173   long current_size = strlen(*buffer);
174   long copy_size    = strlen(&temp_buffer[file_offset]);
175 
176   CeedCall(CeedRealloc(current_size + copy_size + 2, buffer));
177   memcpy(&(*buffer)[current_size], "\n", 2);
178   memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size);
179   memcpy(&(*buffer)[current_size + copy_size + 1], "", 1);
180 
181   // Cleanup
182   CeedCall(CeedFree(&temp_buffer));
183 
184   // Debug
185   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n");
186   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current source file: ");
187   CeedDebug(ceed, "%s\n", source_file_path);
188   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Final buffer:\n");
189   CeedDebug(ceed, "%s\n", *buffer);
190   return CEED_ERROR_SUCCESS;
191 }
192 
193 /**
194   @brief Initialize and load source file into string buffer, including full text of local files in place of `#include "local.h"`.
195 
196   Note: Caller is responsible for freeing the string buffer with @ref CeedFree().
197 
198   @param[in]  ceed             `Ceed` object for error handling
199   @param[in]  source_file_path Absolute path to source file
200   @param[out] buffer           String buffer for source file contents
201 
202   @return An error code: 0 - success, otherwise - failure
203 
204   @ref Backend
205 **/
206 int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path, char **buffer) {
207   // Initialize buffer
208   CeedCall(CeedCalloc(1, buffer));
209 
210   // Load to initalized buffer
211   CeedCall(CeedLoadSourceToInitializedBuffer(ceed, source_file_path, buffer));
212   return CEED_ERROR_SUCCESS;
213 }
214 
215 /**
216   @brief Build an absolute filepath from a base filepath and an absolute filepath.
217 
218   This helps construct source file paths for @ref CeedLoadSourceToBuffer().
219 
220   Note: Caller is responsible for freeing the string buffer with @ref CeedFree().
221 
222   @param[in]  ceed               `Ceed` object for error handling
223   @param[in]  base_file_path     Absolute path to current file
224   @param[in]  relative_file_path Relative path to target file
225   @param[out] new_file_path      String buffer for absolute path to target file
226 
227   @return An error code: 0 - success, otherwise - failure
228 
229   @ref Backend
230 **/
231 int CeedPathConcatenate(Ceed ceed, const char *base_file_path, const char *relative_file_path, char **new_file_path) {
232   char  *last_slash  = strrchr(base_file_path, '/');
233   size_t base_length = (last_slash - base_file_path + 1), relative_length = strlen(relative_file_path),
234          new_file_path_length = base_length + relative_length + 1;
235 
236   CeedCall(CeedCalloc(new_file_path_length, new_file_path));
237   memcpy(*new_file_path, base_file_path, base_length);
238   memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length);
239   return CEED_ERROR_SUCCESS;
240 }
241 
242 /**
243   @brief Find the relative filepath to an installed JiT file
244 
245   @param[in]  absolute_file_path Absolute path to installed JiT file
246   @param[out] relative_file_path Relative path to installed JiT file, a substring of the absolute path
247 
248   @return An error code: 0 - success, otherwise - failure
249 
250   @ref Backend
251 **/
252 int CeedGetJitRelativePath(const char *absolute_file_path, const char **relative_file_path) {
253   *(relative_file_path) = strstr(absolute_file_path, "ceed/jit-source");
254   CeedCheck(*relative_file_path, NULL, CEED_ERROR_MAJOR, "Couldn't find relative path including 'ceed/jit-source' for: %s", absolute_file_path);
255   return CEED_ERROR_SUCCESS;
256 }
257 
258 /**
259   @brief Build an absolute filepath to a JiT file
260 
261   @param[in]  ceed               `Ceed` object for error handling
262   @param[in]  relative_file_path Relative path to installed JiT file
263   @param[out] absolute_file_path String buffer for absolute path to target file, to be freed by caller
264 
265   @return An error code: 0 - success, otherwise - failure
266 
267   @ref Backend
268 **/
269 int CeedGetJitAbsolutePath(Ceed ceed, const char *relative_file_path, const char **absolute_file_path) {
270   Ceed ceed_parent;
271 
272   // Debug
273   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n");
274   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Relative JiT source file: ");
275   CeedDebug(ceed, "%s\n", relative_file_path);
276 
277   CeedCall(CeedGetParent(ceed, &ceed_parent));
278   for (CeedInt i = 0; i < ceed_parent->num_jit_source_roots; i++) {
279     bool is_valid;
280 
281     // Debug
282     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Checking JiT root: ");
283     CeedDebug(ceed, "%s\n", ceed_parent->jit_source_roots[i]);
284 
285     // Build and check absolute path with current root
286     CeedCall(CeedPathConcatenate(ceed, ceed_parent->jit_source_roots[i], relative_file_path, (char **)absolute_file_path));
287     CeedCall(CeedCheckFilePath(ceed, *absolute_file_path, &is_valid));
288 
289     if (is_valid) return CEED_ERROR_SUCCESS;
290     // LCOV_EXCL_START
291     else CeedCall(CeedFree(absolute_file_path));
292     // LCOV_EXCL_STOP
293   }
294   // LCOV_EXCL_START
295   return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't find matching JiT source file: %s", relative_file_path);
296   // LCOV_EXCL_STOP
297 }
298