File: ResolveOCLRaytracingBuiltins.cpp

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (532 lines) | stat: -rw-r--r-- 21,743 bytes parent folder | download
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*========================== begin_copyright_notice ============================

Copyright (C) 2019-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#include "IGC/common/StringMacros.hpp"
#include "IGC/AdaptorCommon/RayTracing/RTBuilder.h"
#include <map>
#include "common/LLVMWarningsPush.hpp"
#include <llvm/IR/IRBuilder.h>
#include "common/LLVMWarningsPop.hpp"
#include "ResolveOCLRaytracingBuiltins.hpp"
#include "Compiler/IGCPassSupport.h"
#include "Compiler/CodeGenPublicEnums.h"
#include "Probe/Assertion.h"
#include "IGC/AdaptorCommon/RayTracing/RTBuilder.h"
#include "IGC/AdaptorCommon/RayTracing/RTStackFormat.h"
#include "IGC/AdaptorCommon/RayTracing/RayTracingRayDispatchGlobalData.h"

using namespace llvm;
using namespace IGC;

// Register pass to igc-opt
#define PASS_FLAG "igc-resolve-ocl-raytracing-builtins"
#define PASS_DESCRIPTION "Resolve OCL raytracing built-ins"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(ResolveOCLRaytracingBuiltins, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_END(ResolveOCLRaytracingBuiltins, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

namespace {
  std::map<std::string, std::function<void(ResolveOCLRaytracingBuiltins*, CallInst&)>> functionHandlersMap = {
    {"__builtin_IB_intel_get_rt_stack",                  &ResolveOCLRaytracingBuiltins::handleGetRtStack            },
    {"__builtin_IB_intel_get_thread_btd_stack",          &ResolveOCLRaytracingBuiltins::handleGetThreadBTDStack     },
    {"__builtin_IB_intel_get_global_btd_stack",          &ResolveOCLRaytracingBuiltins::handleGetGlobalBTDStack     },
    {"__builtin_IB_intel_dispatch_trace_ray_query",      &ResolveOCLRaytracingBuiltins::handleDispatchTraceRayQuery },
    {"__builtin_IB_intel_rt_sync",                       &ResolveOCLRaytracingBuiltins::handleRTSync                },
    {"__builtin_IB_intel_get_implicit_dispatch_globals", &ResolveOCLRaytracingBuiltins::handleGetImplicitDG         },

    // Handling for builtins operating on intel_ray_query_t from intel_rt_production extension
      {"__builtin_IB_intel_init_ray_query",                &ResolveOCLRaytracingBuiltins::handleInitRayQuery          },
      {"__builtin_IB_intel_update_ray_query",              &ResolveOCLRaytracingBuiltins::handleUpdateRayQuery        },
      {"__builtin_IB_intel_query_rt_fence",                &ResolveOCLRaytracingBuiltins::handleQuery                 },
      {"__builtin_IB_intel_query_rt_globals",              &ResolveOCLRaytracingBuiltins::handleQuery                 },
      {"__builtin_IB_intel_query_rt_stack",                &ResolveOCLRaytracingBuiltins::handleQuery                 },
      {"__builtin_IB_intel_query_ctrl",                    &ResolveOCLRaytracingBuiltins::handleQuery                 },
      {"__builtin_IB_intel_query_bvh_level",               &ResolveOCLRaytracingBuiltins::handleQuery                 },
  };
}

char ResolveOCLRaytracingBuiltins::ID = 0;

ResolveOCLRaytracingBuiltins::ResolveOCLRaytracingBuiltins() :
    ModulePass(ID), m_pCtx(nullptr), m_builder(nullptr) {
    initializeResolveOCLRaytracingBuiltinsPass(*PassRegistry::getPassRegistry());
}

bool ResolveOCLRaytracingBuiltins::runOnModule(Module& M) {
    m_pCtx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
    m_callsToReplace.clear();

    IGCIRBuilder<> builder(M.getContext());
    m_builder = &builder;

    // intel_rt_production extension uses intel_ray_query_opaque_t which cannot be defined in BiF Library
    defineOpaqueTypes();

    // Fills up the m_CallsToReplace with all instances of calls to kernels in the functionHandlersMap.
    visit(M);

    if (m_callsToReplace.size() > 0) {
        if (m_pCtx->platform.getPlatformInfo().eRenderCoreFamily != IGFX_XE_HPC_CORE &&
            m_pCtx->platform.getPlatformInfo().eProductFamily != IGFX_DG2) {
            IGC_ASSERT_MESSAGE(0, "Raytracing extensions used on unsupported platform!");
            m_pCtx->EmitError("OCL raytracing extensions can be used only on supported platform", *m_callsToReplace.begin());
            return false;
        }

        constexpr uint32_t AllocSize = RTStackFormat::getSyncStackSize();
        m_pCtx->getModuleMetaData()->rtInfo.RayQueryAllocSizeInBytes = AllocSize;
    }

    bool found_regular_function = false;
    auto& FuncMap = m_pCtx->getModuleMetaData()->FuncMD;
    // the vector contains RT calls inside both kernels and regular functiuons
    for (auto p : m_callsToReplace) {
        IGC_ASSERT(nullptr != p);  // All code below assumes that it catches the null pointers.
        m_builder->SetInsertPoint(p);

        // Set the hasSyncRTCalls for each FunctionMetaData of a Function which is in the ModuleMetaData.
        // We will be reading that value later on in the code.
        auto FuncIter = FuncMap.find(p->getFunction());
        if (FuncIter != FuncMap.end()) {
            // metadata for that function/kernel was found
            // usually all the kernels have their metadata saved
            FunctionMetaData& funcMD = FuncIter->second;
            if (funcMD.functionType == KernelFunction) {
                // is a kernel
                funcMD.hasSyncRTCalls = true;
            }
            else {
                // is a regular function
                funcMD.hasSyncRTCalls = true;
                found_regular_function = true;
            }
        }
        else {
            // metadata for that function/kernel was not found
            // likely this is a regular function, not a kernel
            found_regular_function = true;
        }

        // Dereference that function pointer at that location in the map.
        (functionHandlersMap.at( p->getCalledFunction()->getName().str() ))(this, *p);
    }

    // If we found RT calls in a regular function, that regular function could have been called by a kernel.
    // In that case the kernel also exhibits ray tracing functionality.
    // For now just loop through all the kernels (that we have metadata for) and assume that any one of them may be the caller.
    // Marking them all as having ray tracing calls seems to be not harmful.
    // TODO: In future implementation go back up the call graph from the regular function with hasSyncRTCalls until you find the parent kernel.
    if (found_regular_function) {
        for (auto& md_entry : FuncMap) {
            md_entry.second.hasSyncRTCalls = true;
        }
    }

    return m_callsToReplace.size() > 0;
}

void ResolveOCLRaytracingBuiltins::defineOpaqueTypes(){
    Module* M = m_pCtx->getModule();
    LLVMContext& C = M->getContext();

    StructType* rayQueryTy = IGCLLVM::getTypeByName(*M, "struct.intel_ray_query_opaque_t");

    if (!rayQueryTy) return;

    auto getOrCreateOpaqueType = [](Module* M, const std::string& Name) {
        StructType* opaqueType = IGCLLVM::getTypeByName(*M, Name);
        if (!opaqueType)
            opaqueType = StructType::create(M->getContext(), Name);
        return opaqueType;
    };

    StructType* rtFenceTy = getOrCreateOpaqueType(M, "struct.rtfence_t");
    StructType* rtGlobalsTy = getOrCreateOpaqueType(M, "struct.rtglobals_t");

    IGC_ASSERT(rtFenceTy && rtGlobalsTy);

    SmallVector<Type*, 4> Tys{
        PointerType::get(rtFenceTy, ADDRESS_SPACE_PRIVATE),
        PointerType::get(rtGlobalsTy, ADDRESS_SPACE_GLOBAL),
        PointerType::get(Type::getInt8Ty(C), ADDRESS_SPACE_GLOBAL),
        Type::getInt32Ty(C),
        Type::getInt32Ty(C)
    };

    rayQueryTy->setBody(Tys);
}

void ResolveOCLRaytracingBuiltins::visitCallInst(CallInst& callInst) {
  if (!callInst.getCalledFunction()) return;
  if (functionHandlersMap.find(callInst.getCalledFunction()->getName().str()) != functionHandlersMap.end()) {
    m_callsToReplace.push_back(&callInst);
  }
}

/*
Handler for:
void* __builtin_IB_intel_get_rt_stack( rtglobals_t rt_dispatch_globals );

Description:
Returns a pointer to the data structure which the RT hardware operates on.
The RT Stack address is computed as:
    syncStackSize = sizeof(HitInfo)*2 + (sizeof(Ray) + sizeof(TravStack))*RTDispatchGlobals.maxBVHLevels;
    syncBase = RTDispatchGlobals.rtMemBasePtr - (DSSID * NUM_SIMD_LANES_PER_DSS + StackID + 1)*syncStackSize;
Where DSSID is an index which uniquely identifies the DSS in the machine (across tiles), and StackID is compute as below:
    With fused EUs (e.g. in DG2) :
      StackID[10:0] (msb to lsb) = (EUID[3:0]<<7) | (THREAD_ID[2:0]<<4) | SIMD_LANE_ID[3:0]

    With natively wide EUs(e.g. in PVC):
      StackID[10:0] (msb to lsb) = (EUID[2:0]<<8) | (THREAD_ID[3:0]<<4) | SIMD_LANE_ID[3:0]

*/
void ResolveOCLRaytracingBuiltins::handleGetRtStack(CallInst& callInst) {
  IGC_ASSERT(callInst.getType()->isPointerTy());
  auto rtDispatchGlobals = callInst.getArgOperand(0);

  // Calculate:
  // syncBase = RTDispatchGlobals.rtMemBasePtr - (DSSID * NUM_SIMD_LANES_PER_DSS + StackID + 1)*syncStackSize;
  RTBuilder rtbuilder(m_builder->getContext(), *m_pCtx);
  rtbuilder.SetInsertPoint(&callInst);

  // RTBuilder uses GEP instructions on the global pointer for some operations.
  // Cast explicit global buffer pointer type to the type used by RTBuilder.
  auto GlobalPtrTy = rtbuilder.getRayDispatchGlobalDataPtrTy(*callInst.getModule());
  rtDispatchGlobals = rtbuilder.CreatePointerCast(rtDispatchGlobals, GlobalPtrTy);

  // By default RTBuilder uses an implicit global buffer pointer.
  // OCL extensions use explicit buffer.
  rtbuilder.setGlobalBufferPtr(rtDispatchGlobals);

  // Disable optimisation used on DX path.
  rtbuilder.setDisableRTGlobalsKnownValues(true);

  auto rtMemBasePtr = rtMemBasePtrGetter(rtDispatchGlobals);
  Value* stackOffset = rtbuilder.CreateZExt(rtbuilder.getSyncStackOffset(), rtMemBasePtr->getType());
  Value* syncBase = m_builder->CreateSub(rtMemBasePtr, stackOffset, "syncBase");

  // Cast to resulting pointer
  syncBase = m_builder->CreateIntToPtr(syncBase, callInst.getType());
  callInst.replaceAllUsesWith(syncBase);
  callInst.eraseFromParent();
}

/*
Handler for
void* __builtin_IB_intel_get_thread_btd_stack( rtglobals_t rt_dispatch_globals );

Description:
Returns a pointer to the extra per-thread storage for the given stack.
This is computed as:
  local_stack = rt_dispatch_globals->rtMemBasePtr +
                   rt_dispatch_globals->stackSizePerRay * 64 *
                     (BTD_Stack_ID + DSSID * rt_dispatch_globals->numDSSRTStacks) ;

The BTD_Stack_ID is the StackID which is allocated by hardware when BTD is enabled.

*/
void ResolveOCLRaytracingBuiltins::handleGetThreadBTDStack(CallInst& callInst) {
  handleGetBTDStack(callInst, false);
}

/*
Handler for
void* __builtin_IB_intel_get_global_btd_stack( rtglobals_t rt_dispatch_globals );

Description:
Returns a pointer to the extra global storage for the given stack.
This is computed as:

global_extra = rt_dispatch_globals->rtMemBasePtr +
                 rt_dispatch_globals->stackSizePerRay * 64 *
                 rt_dispatch_globals->numDSSRTStacks  *
                 DSS_COUNT;
*/
void ResolveOCLRaytracingBuiltins::handleGetGlobalBTDStack(CallInst& callInst) {
  handleGetBTDStack(callInst, true);
}

void ResolveOCLRaytracingBuiltins::handleGetBTDStack(CallInst& callInst, const bool isGlobal) {
  IGC_ASSERT(callInst.getType()->isPointerTy());
  auto rtDispatchGlobals = callInst.getArgOperand(0);
  Value* rtMemBasePtr = rtMemBasePtrGetter(rtDispatchGlobals);
  Value* stackSizePerRay = stackSizePerRayGetter(rtDispatchGlobals);
  Value* numDSSStacks = numDSSRTStacksGetter(rtDispatchGlobals);
  Value* dssID = getIntrinsicValue(GenISAIntrinsic::GenISA_dual_subslice_id);

  stackSizePerRay = m_builder->CreateZExt(stackSizePerRay, m_builder->getInt64Ty());
  numDSSStacks = m_builder->CreateZExt(numDSSStacks, m_builder->getInt64Ty());
  dssID = m_builder->CreateZExt(dssID, m_builder->getInt64Ty());

  Value* stack = m_builder->CreateMul(dssID, numDSSStacks);
  if (!isGlobal) {
    Value* btdStackID = getIntrinsicValue(GenISAIntrinsic::GenISA_AsyncStackID);
    btdStackID = m_builder->CreateZExt(btdStackID, m_builder->getInt64Ty());
    stack = m_builder->CreateAdd(stack, btdStackID);
  }
  stack = m_builder->CreateMul(stack, m_builder->getInt64(64));
  stack = m_builder->CreateMul(stack, stackSizePerRay);
  stack = m_builder->CreateAdd(stack, rtMemBasePtr);

  stack = m_builder->CreateIntToPtr(stack, callInst.getType());

  callInst.replaceAllUsesWith(stack);
  callInst.eraseFromParent();
}

// Note: this can be merged with RTBuilder::CreateLSCFence()
// once we use RTBuilder in this file.
CallInst* ResolveOCLRaytracingBuiltins::CreateLSCFence(
    llvm::IRBuilder<>* IRB,
    LSC_SFID SFID,
    LSC_SCOPE Scope,
    LSC_FENCE_OP FenceOp)
{
    Function* pFunc = GenISAIntrinsic::getDeclaration(
        IRB->GetInsertBlock()->getModule(),
        GenISAIntrinsic::GenISA_LSCFence);

    Value* VSFID  = IRB->getInt32(SFID);
    Value* VScope = IRB->getInt32(Scope);
    Value* VOp    = IRB->getInt32(FenceOp);

    Value* Args[] =
    {
        VSFID, VScope, VOp
    };

    return IRB->CreateCall(pFunc, Args);
}

/*
Handler for
rtfence_t __builtin_IB_intel_dispatch_trace_ray_query(
  rtglobals_t rt_dispatch_globals, uint bvh_level, uint traceRayCtrl);

Description:
Invokes the RT HW within EU kernel code.
The RTDispatchGlobals argument is required to be uniform across the subgroup.
The BVH Level argument is used for software instancing.
The Trace Ray Control argument is an enumerant controlling the RT HW.

The compiler will implement this function by assembling the inputs into a RT message.
The return value of this function is a sync object which will be used by the kernel to synchronize the RT message.
*/
void ResolveOCLRaytracingBuiltins::handleDispatchTraceRayQuery(CallInst& callInst) {
  IGC_ASSERT(callInst.getType()->isPointerTy());
  IGC_ASSERT(IGCLLVM::getNumArgOperands(&callInst) == 3);

  // Insert a ugm fence prior to send.rta to ensure RTUnit has accesss to
  // current data.
  CreateLSCFence(m_builder, LSC_UGM, LSC_SCOPE_LOCAL, LSC_FENCE_OP_NONE);

  auto rtDispatchGlobals = callInst.getArgOperand(0);
  auto bvhLevel = callInst.getArgOperand(1);
  auto traceRayCtrl = callInst.getArgOperand(2);

  // Prepare the payload
  // [0:2] bvh_level
  bvhLevel = m_builder->CreateAnd(bvhLevel, 7);
  // [8:9] trace_ray_control
  traceRayCtrl = m_builder->CreateAnd(traceRayCtrl, 3);
  traceRayCtrl = m_builder->CreateShl(traceRayCtrl, 8);
  // [26:16] stack_id.
  // for RayQuery this is not used, leave it as 0.

  Value* payload = m_builder->CreateOr(bvhLevel, traceRayCtrl, VALUE_NAME("traceRayQueryPayload"));

  Value* Args[] = {
      rtDispatchGlobals,
      payload
  };

  // The return value from this TraceRay with RayQueryEnable is a dummy register, which is used
  // to sync this message via scoreboard.
  auto fenceValue = getIntrinsicValue(GenISAIntrinsic::GenISA_TraceRaySync, Args);
  fenceValue = m_builder->CreateIntToPtr(fenceValue, callInst.getType());
  if (m_pCtx->platform.RTFenceWAforBkModeEnabled())
  {
      CreateLSCFence(m_builder, LSC_UGM, LSC_SCOPE_GPU, LSC_FENCE_OP_EVICT);
  }
  callInst.replaceAllUsesWith(fenceValue);
  callInst.eraseFromParent();
}

/*
Handler for
void __builtin_IB_intel_rt_sync(rtfence_t fence);

Description:
Ensures the asynchronous ray query is complete.
The kernel must pass the fence object returned by the ray dispatch query function
to this sync function prior to reading any results from the RT Stack
*/
void ResolveOCLRaytracingBuiltins::handleRTSync(CallInst& callInst) {
  auto fence = m_builder->CreatePtrToInt(callInst.getOperand(0), m_builder->getInt32Ty());
  getIntrinsicValue(GenISAIntrinsic::GenISA_ReadTraceRaySync, fence);
  callInst.eraseFromParent();
}

/*
Handler for
void __builtin_IB_intel_get_implicit_dispatch_globals();

Description:
Returns IMPLICIT_RT_GLOBAL_BUFFER implicit argument.
*/
void ResolveOCLRaytracingBuiltins::handleGetImplicitDG(llvm::CallInst& callInst) {
    RTBuilder rtbuilder(m_builder->getContext(), *m_pCtx);
    rtbuilder.SetInsertPoint(&callInst);
    auto v = rtbuilder.getGlobalBufferPtr();
    auto c = rtbuilder.CreateBitCast(v, callInst.getType());
    callInst.replaceAllUsesWith(c);
    callInst.eraseFromParent();
}

/*
Handler for
void __builtin_IB_intel_init_ray_query(
    rtfence_t, rtglobals_t,global RTStack*, TraceRayCtrl, uint);

Description:
Allocates private memory for rayquery object and stores all it's initializer
values passed as argument to __builtin_IB_intel_init_ray_query.
*/
void ResolveOCLRaytracingBuiltins::handleInitRayQuery(llvm::CallInst& callInst) {
    RTBuilder rtbuilder(m_builder->getContext(), *m_pCtx);
    Function* F = callInst.getFunction();
    rtbuilder.SetInsertPoint(&*F->getEntryBlock().getFirstInsertionPt());

    unsigned numArgs = IGCLLVM::getNumArgOperands(&callInst);
    IGC_ASSERT(numArgs == 5);

    auto* allocaType = callInst.getType()->getPointerElementType();
    auto* alloca = rtbuilder.CreateAlloca(allocaType);

    rtbuilder.SetInsertPoint(&callInst);

    auto storeToAlloca = [&](unsigned argIndex)
    {
        auto ptr = rtbuilder.CreateGEP(alloca, { rtbuilder.getInt32(0), rtbuilder.getInt32(argIndex) });
        auto arg = callInst.getOperand(argIndex);
        rtbuilder.CreateStore(arg, ptr);
    };

    for (unsigned argIndex = 0; argIndex < numArgs; argIndex++)
        storeToAlloca(argIndex);

    callInst.replaceAllUsesWith(alloca);
    callInst.eraseFromParent();
}

/*
Handler for
void __builtin_IB_intel_update_ray_query(
    intel_ray_query_t, rtfence_t, rtglobals_t, global RTStack*, TraceRayCtrl, uint);

Description:
Stores new values to rayquery alloca
*/
void ResolveOCLRaytracingBuiltins::handleUpdateRayQuery(llvm::CallInst& callInst) {
    RTBuilder rtbuilder(m_builder->getContext(), *m_pCtx);
    rtbuilder.SetInsertPoint(&callInst);

    unsigned numArgs = IGCLLVM::getNumArgOperands(&callInst);
    IGC_ASSERT(numArgs == 6);

    Value* rayQuery = callInst.getOperand(0);

    for (unsigned argIndex = 1; argIndex < numArgs; argIndex++)
    {
        auto* ptr = rtbuilder.CreateGEP(rayQuery, { rtbuilder.getInt32(0), rtbuilder.getInt32(argIndex - 1) });
        Value* arg = callInst.getOperand(argIndex);
        rtbuilder.CreateStore(arg, ptr);
    }

    callInst.eraseFromParent();
}

/*
Handler for the following builtins:
rtfence_t        __builtin_IB_intel_query_rt_fence(intel_ray_query_t);
rtglobals_t      __builtin_IB_intel_query_rt_globals(intel_ray_query_t);
global RTStack*  __builtin_IB_intel_query_rt_stack(intel_ray_query_t);
TraceRayCtrl     __builtin_IB_intel_query_ctrl(intel_ray_query_t);
uint             __builtin_IB_intel_query_bvh_level(intel_ray_query_t);

Description:
Loads queried value from rayquery alloca
*/
void ResolveOCLRaytracingBuiltins::handleQuery(llvm::CallInst& callInst) {
    RTBuilder rtbuilder(m_builder->getContext(), *m_pCtx);
    rtbuilder.SetInsertPoint(&callInst);

    enum RayQueryArgsOrder
    {
        RT_FENCE,
        RT_GLOBALS,
        RT_STACK,
        CTRL,
        BVH_LEVEL
    };

    static const std::map<std::string, RayQueryArgsOrder> builtinToArgIndex = {
        { "__builtin_IB_intel_query_rt_fence",   RT_FENCE },
        { "__builtin_IB_intel_query_rt_globals", RT_GLOBALS },
        { "__builtin_IB_intel_query_rt_stack",   RT_STACK },
        { "__builtin_IB_intel_query_ctrl",       CTRL },
        { "__builtin_IB_intel_query_bvh_level",  BVH_LEVEL}
    };

    IGC_ASSERT(IGCLLVM::getNumArgOperands(&callInst) == 1);
    Value* rayQuery = callInst.getArgOperand(0);

    unsigned argIndex = builtinToArgIndex.at(callInst.getCalledFunction()->getName().str());
    auto* ptr = rtbuilder.CreateGEP(rayQuery, { rtbuilder.getInt32(0), rtbuilder.getInt32(argIndex) });
    auto* queriedValue = rtbuilder.CreateLoad(ptr);

    callInst.replaceAllUsesWith(queriedValue);
    callInst.eraseFromParent();
}

// ---- Helper functions ----

Instruction* ResolveOCLRaytracingBuiltins::loadFromOffset(Value* basePtr, const size_t offset, const size_t typeSizeInBytes, StringRef valName = "") {
  IGC_ASSERT(isa<PointerType>(basePtr->getType()));
  Value* ptrAsInt = m_builder->CreatePtrToInt(basePtr, m_builder->getInt64Ty());
  Value* intWithOffset = m_builder->CreateAdd(ptrAsInt, m_builder->getInt64(offset));
  Type* resType = m_builder->getIntNTy(typeSizeInBytes * 8);
  Value* ptrWithOffset = m_builder->CreateIntToPtr(intWithOffset,
    PointerType::get(resType, basePtr->getType()->getPointerAddressSpace()));
  Instruction* loadedVal = m_builder->CreateLoad(ptrWithOffset, valName);
  return loadedVal;
}

#define RT_DISPATCH_GETTER_DEF(FieldName) \
Instruction* ResolveOCLRaytracingBuiltins::FieldName##Getter(Value* rtDispatchGlobalsValue) {  \
  constexpr size_t offset = offsetof(RayDispatchGlobalData, FieldName);                        \
  constexpr size_t typeSize = sizeof(((RayDispatchGlobalData*)0)->FieldName);                  \
  return loadFromOffset(rtDispatchGlobalsValue, offset, typeSize, #FieldName);                 \
}

RT_DISPATCH_GETTER_DEF(rtMemBasePtr)
RT_DISPATCH_GETTER_DEF(maxBVHLevels)
RT_DISPATCH_GETTER_DEF(stackSizePerRay)
RT_DISPATCH_GETTER_DEF(numDSSRTStacks)

#undef RT_DISPATCH_GETTER_DEF

Value* ResolveOCLRaytracingBuiltins::getIntrinsicValue(GenISAIntrinsic::ID intrinsicId, ArrayRef<Value*> args) {
  std::vector<Type*> types;
  if (!args.empty()) {
    std::transform(args.begin(), args.end(), std::back_inserter(types), [](Value* v) { return v->getType(); });
  }
  return m_builder->CreateCall(GenISAIntrinsic::getDeclaration(m_builder->GetInsertPoint()->getModule(), intrinsicId, types), args);
}