summaryrefslogtreecommitdiffstats
path: root/tools/slang-unit-test/unit-test-ir-blob.cpp
blob: 6f72afae5253516a7110f4169be7cd73356cc89f (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// unit-test-ir-blob.cpp

#include "slang-com-ptr.h"
#include "slang.h"
#include "unit-test/slang-unit-test.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace Slang;

// Test the slang_loadModuleFromIRBlob and slang_loadModuleInfoFromIRBlob functions
SLANG_UNIT_TEST(irBlob)
{
    // Test source code for creating IR data
    const char* testModuleSource = R"(
        module test_ir_module;

        public struct TestStruct {
            float x, y, z;
        }

        public void testFunction(TestStruct input) {
            // Simple function
        }

        public static const float PI = 3.14159;
    )";

    ComPtr<slang::IGlobalSession> globalSession;
    SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);

    slang::SessionDesc sessionDesc = {};
    sessionDesc.targetCount = 1;
    slang::TargetDesc targetDesc = {};
    targetDesc.format = SLANG_SPIRV;
    targetDesc.profile = globalSession->findProfile("spirv_1_5");
    sessionDesc.targets = &targetDesc;

    ComPtr<slang::ISession> session;
    SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);

    // Create IR data by serializing a module
    ComPtr<ISlangBlob> irBlob;
    {
        ComPtr<slang::IModule> module;
        ComPtr<ISlangBlob> diagnostics;

        module = session->loadModuleFromSourceString(
            "test_ir_module",
            "test_ir_module.slang",
            testModuleSource,
            diagnostics.writeRef());

        SLANG_CHECK(module != nullptr);
        if (diagnostics && diagnostics->getBufferSize() > 0)
        {
            // Log diagnostics if any
            printf(
                "Module compilation diagnostics: %.*s\n",
                (int)diagnostics->getBufferSize(),
                (const char*)diagnostics->getBufferPointer());
        }

        // Serialize the module to create IR data
        SLANG_CHECK(module->serialize(irBlob.writeRef()) == SLANG_OK);
        SLANG_CHECK(irBlob != nullptr);
        SLANG_CHECK(irBlob->getBufferSize() > 0);
    }

    // Test 1: Test slang_loadModuleFromIRBlob with valid IR data
    {
        ComPtr<slang::IModule> loadedModule;
        ComPtr<ISlangBlob> diagnostics;

        loadedModule = slang_loadModuleFromIRBlob(
            session,
            "test_ir_module_loaded",
            "test_ir_module_loaded.slang",
            irBlob->getBufferPointer(),
            irBlob->getBufferSize(),
            diagnostics.writeRef());

        SLANG_CHECK(loadedModule != nullptr);
        if (diagnostics && diagnostics->getBufferSize() > 0)
        {
            // Log diagnostics if any
            printf(
                "IR blob loading diagnostics: %.*s\n",
                (int)diagnostics->getBufferSize(),
                (const char*)diagnostics->getBufferPointer());
        }

        // Verify the loaded module is valid
        SLANG_CHECK(loadedModule != nullptr);
    }

    // Test 2: Test slang_loadModuleInfoFromIRBlob with valid IR data
    {
        SlangInt moduleVersion;
        const char* moduleCompilerVersion;
        const char* moduleName;

        SlangResult result = slang_loadModuleInfoFromIRBlob(
            session,
            irBlob->getBufferPointer(),
            irBlob->getBufferSize(),
            moduleVersion,
            moduleCompilerVersion,
            moduleName);

        SLANG_CHECK(result == SLANG_OK);
        SLANG_CHECK(moduleName != nullptr);
        SLANG_CHECK(strcmp(moduleName, "test_ir_module") == 0);
        SLANG_CHECK(moduleCompilerVersion != nullptr);
        SLANG_CHECK(moduleVersion >= 0);
    }

    // Test 3: Test slang_loadModuleFromIRBlob with invalid parameters
    {
        ComPtr<slang::IModule> module;
        ComPtr<ISlangBlob> diagnostics;

        // Test with null session
        module = slang_loadModuleFromIRBlob(
            nullptr,
            "testModule",
            "test.slang",
            irBlob->getBufferPointer(),
            irBlob->getBufferSize(),
            diagnostics.writeRef());

        SLANG_CHECK(module == nullptr);

        // Test with null moduleName
        module = slang_loadModuleFromIRBlob(
            session,
            nullptr,
            "test.slang",
            irBlob->getBufferPointer(),
            irBlob->getBufferSize(),
            diagnostics.writeRef());

        SLANG_CHECK(module == nullptr);

        // Test with null path
        module = slang_loadModuleFromIRBlob(
            session,
            "testModule",
            nullptr,
            irBlob->getBufferPointer(),
            irBlob->getBufferSize(),
            diagnostics.writeRef());

        SLANG_CHECK(module == nullptr);

        // Test with null source
        module = slang_loadModuleFromIRBlob(
            session,
            "testModule",
            "test.slang",
            nullptr,
            irBlob->getBufferSize(),
            diagnostics.writeRef());

        SLANG_CHECK(module == nullptr);

        // Test with zero size
        module = slang_loadModuleFromIRBlob(
            session,
            "testModule",
            "test.slang",
            irBlob->getBufferPointer(),
            0,
            diagnostics.writeRef());

        SLANG_CHECK(module == nullptr);
    }

    // Test 4: Test slang_loadModuleInfoFromIRBlob with invalid parameters
    {
        SlangInt moduleVersion;
        const char* moduleCompilerVersion;
        const char* moduleName;

        // Test with null session
        SlangResult result = slang_loadModuleInfoFromIRBlob(
            nullptr,
            irBlob->getBufferPointer(),
            irBlob->getBufferSize(),
            moduleVersion,
            moduleCompilerVersion,
            moduleName);

        SLANG_CHECK(result == SLANG_E_INVALID_ARG);

        // Test with null source
        result = slang_loadModuleInfoFromIRBlob(
            session,
            nullptr,
            irBlob->getBufferSize(),
            moduleVersion,
            moduleCompilerVersion,
            moduleName);

        SLANG_CHECK(result == SLANG_E_INVALID_ARG);

        // Test with zero size
        result = slang_loadModuleInfoFromIRBlob(
            session,
            irBlob->getBufferPointer(),
            0,
            moduleVersion,
            moduleCompilerVersion,
            moduleName);

        SLANG_CHECK(result == SLANG_E_INVALID_ARG);
    }

    // Test 5: Test with corrupted/invalid IR data
    {
        ComPtr<slang::IModule> module;
        ComPtr<ISlangBlob> diagnostics;

        // Create some invalid data
        const char* invalidData = "This is not valid IR data";
        size_t invalidDataSize = strlen(invalidData);

        module = slang_loadModuleFromIRBlob(
            session,
            "testModule",
            "test.slang",
            invalidData,
            invalidDataSize,
            diagnostics.writeRef());

        // This might return nullptr or a module with diagnostics
        if (module == nullptr)
        {
            // If it failed, that's expected for invalid data
            SLANG_CHECK(true);
        }
        else
        {
            // If it succeeded, there should be diagnostics
            if (diagnostics && diagnostics->getBufferSize() > 0)
            {
                SLANG_CHECK(true);
            }
        }
    }

    // Test 6: Test slang_loadModuleInfoFromIRBlob with corrupted/invalid IR data
    {
        SlangInt moduleVersion;
        const char* moduleCompilerVersion;
        const char* moduleName;

        // Create some invalid data
        const char* invalidData = "This is not valid IR data";
        size_t invalidDataSize = strlen(invalidData);

        SlangResult result = slang_loadModuleInfoFromIRBlob(
            session,
            invalidData,
            invalidDataSize,
            moduleVersion,
            moduleCompilerVersion,
            moduleName);

        // This should fail with invalid data
        SLANG_CHECK(result != SLANG_OK);
    }

    // Test 7: Test round-trip serialization and loading
    {
        // Load the module from IR
        ComPtr<slang::IModule> loadedModule;
        ComPtr<ISlangBlob> diagnostics;

        loadedModule = slang_loadModuleFromIRBlob(
            session,
            "test_round_trip",
            "test_round_trip.slang",
            irBlob->getBufferPointer(),
            irBlob->getBufferSize(),
            diagnostics.writeRef());

        SLANG_CHECK(loadedModule != nullptr);

        if (loadedModule)
        {
            // Serialize the loaded module again
            ComPtr<ISlangBlob> roundTripBlob;
            SLANG_CHECK(loadedModule->serialize(roundTripBlob.writeRef()) == SLANG_OK);
            SLANG_CHECK(roundTripBlob != nullptr);
            SLANG_CHECK(roundTripBlob->getBufferSize() > 0);

            // Load it again
            ComPtr<slang::IModule> roundTripModule;
            roundTripModule = slang_loadModuleFromIRBlob(
                session,
                "test_round_trip_2",
                "test_round_trip_2.slang",
                roundTripBlob->getBufferPointer(),
                roundTripBlob->getBufferSize(),
                diagnostics.writeRef());

            SLANG_CHECK(roundTripModule != nullptr);
        }
    }

    // Test 8: Test multiple modules with different IR data
    {
        // Create a second module with different content
        const char* testModuleSource2 = R"(
            module test_ir_module_2;

            public struct AnotherStruct {
                int a, b, c;
            }

            public void anotherFunction(AnotherStruct input) {
                // Another function
            }
        )";

        ComPtr<slang::IModule> module2;
        ComPtr<ISlangBlob> diagnostics2;
        ComPtr<ISlangBlob> irBlob2;

        module2 = session->loadModuleFromSourceString(
            "test_ir_module_2",
            "test_ir_module_2.slang",
            testModuleSource2,
            diagnostics2.writeRef());

        SLANG_CHECK(module2 != nullptr);
        SLANG_CHECK(module2->serialize(irBlob2.writeRef()) == SLANG_OK);

        // Load both modules
        ComPtr<slang::IModule> loadedModule1;
        ComPtr<slang::IModule> loadedModule2;
        ComPtr<ISlangBlob> diagnostics;

        loadedModule1 = slang_loadModuleFromIRBlob(
            session,
            "test_ir_module_1_loaded",
            "test_ir_module_1_loaded.slang",
            irBlob->getBufferPointer(),
            irBlob->getBufferSize(),
            diagnostics.writeRef());

        loadedModule2 = slang_loadModuleFromIRBlob(
            session,
            "test_ir_module_2_loaded",
            "test_ir_module_2_loaded.slang",
            irBlob2->getBufferPointer(),
            irBlob2->getBufferSize(),
            diagnostics.writeRef());

        SLANG_CHECK(loadedModule1 != nullptr);
        SLANG_CHECK(loadedModule2 != nullptr);

        // Verify both modules loaded successfully
        SLANG_CHECK(loadedModule1 != nullptr);
        SLANG_CHECK(loadedModule2 != nullptr);
    }
}