summaryrefslogtreecommitdiff
path: root/source/slang/slang-hlsl-intrinsic-set.cpp
blob: 8c7e4fc4ca23d2be8b4d6f4fce27ef6b30e99874 (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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
// slang-hlsl-intrinsic-set.cpp
#include "slang-hlsl-intrinsic-set.h"

#include "slang-ir.h"
#include "slang-ir-insts.h"

namespace Slang
{

/* static */const HLSLIntrinsic::Info HLSLIntrinsic::s_operationInfos[] =
{
#define SLANG_HLSL_INTRINSIC_OP_INFO(x, funcName, numOperands) { UnownedStringSlice::fromLiteral(#x), UnownedStringSlice::fromLiteral(funcName), int8_t(numOperands)  },
    SLANG_HLSL_INTRINSIC_OP(SLANG_HLSL_INTRINSIC_OP_INFO)
};

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!! HLSLIntrinsicSet !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

HLSLIntrinsicSet::HLSLIntrinsicSet(IRTypeSet* typeSet, HLSLIntrinsicOpLookup* lookup):
    m_intrinsicFreeList(sizeof(HLSLIntrinsic), SLANG_ALIGN_OF(HLSLIntrinsic), 1024),
    m_typeSet(typeSet),
    m_opLookup(lookup)
{
}

static IRBasicType* _getElementType(IRType* type)
{
    switch (type->getOp())
    {
        case kIROp_VectorType:      type = static_cast<IRVectorType*>(type)->getElementType(); break;
        case kIROp_MatrixType:      type = static_cast<IRMatrixType*>(type)->getElementType(); break;
        default:                    break;
    }
    return dynamicCast<IRBasicType>(type);
}

void HLSLIntrinsicSet::_calcIntrinsic(HLSLIntrinsic::Op op, IRType* returnType, IRType*const* inArgs, Index argsCount, HLSLIntrinsic& out)
{
    IRBuilder& builder = m_typeSet->getBuilder();

    // Check all types belong to the module

    IRModule* module = builder.getModule();

    SLANG_UNUSED(module);
    SLANG_ASSERT(returnType->getModule() == module);

    for (Index i = 0; i < argsCount; ++i)
    {
        SLANG_ASSERT(inArgs[i]->getModule() == module);
    }

    // Set up the out
    out.op = op;
    out.returnType = returnType;

    switch (op)
    {
        case Op::GetAt:
        {
            IRType* argTypes[3];

            SLANG_ASSERT(argsCount == 2 || argsCount == 3);
            // TODO(JS):
            // HACK! GetAt can be from getElementPtr or from getElement. Get element ptr means the return type will be
            // a pointer. We don't want to deal with that, so strip it
            if (returnType->getOp() == kIROp_PtrType)
            {
                returnType = as<IRType>(returnType->getOperand(0));
            }

            // TODO(JS): Similarly for the input parameters 
            for (Index i = 0; i < argsCount; ++i)
            {
                IRType* argType = inArgs[i];

                if (argType->getOp() == kIROp_PtrType)
                {
                    argType = as<IRType>(argType->getOperand(0));
                }
                argTypes[i] = argType;
            }

            out.returnType = returnType;
            out.signatureType = builder.getFuncType(argsCount, argTypes, builder.getVoidType());
            break;
        }
        case Op::ConstructFromScalar:
        {
            //SLANG_ASSERT(argsCount == 1);
            SLANG_ASSERT(argsCount == 1);
            IRType* srcType = _getElementType(returnType);
            IRType* argTypes[2] = { returnType, srcType };

            out.signatureType = builder.getFuncType(2, argTypes, builder.getVoidType());
            break;
        }
        case Op::ConstructConvert:
        {
            // Make the return type a parameter, to make the signature take into account 
            SLANG_ASSERT(argsCount == 1);
            IRType* argTypes[2] = { returnType, inArgs[0] };

            out.signatureType = builder.getFuncType(2, argTypes, builder.getVoidType());
            break;
        }
        default:
        {
            out.signatureType = builder.getFuncType(argsCount, inArgs, builder.getVoidType());
            break;
        }
    }
}

void HLSLIntrinsicSet::calcIntrinsic(HLSLIntrinsic::Op op, IRType* returnType, IRType*const* inArgTypes, Index argCount, HLSLIntrinsic& out)
{
    returnType = m_typeSet->getType(returnType);

    if (argCount <= 8)
    {
        IRType* args[8];
        for (Index i = 0; i < argCount; ++i)
        {
            args[i] = m_typeSet->getType(inArgTypes[i]);
        }
        _calcIntrinsic(op, returnType, args, argCount, out);
    }
    else
    {
        List<IRType*> args;
        args.setCount(argCount);

        for (Index i = 0; i < argCount; ++i)
        {
            args[i] = m_typeSet->getType(inArgTypes[i]);
        }
        _calcIntrinsic(op, returnType, args.getBuffer(), argCount, out);
    }
}

void HLSLIntrinsicSet::calcIntrinsic(HLSLIntrinsic::Op op, IRInst* inst, Index operandCount, HLSLIntrinsic& out)
{
    IRType* returnType = m_typeSet->getType(inst->getDataType());
    if (operandCount <= 8)
    {
        IRType* argTypes[8];
        for (Index i = 0; i < operandCount; ++i)
        {
            auto operand = inst->getOperand(i);
            argTypes[i] = m_typeSet->getType(operand->getDataType());
        }
        _calcIntrinsic(op, returnType, argTypes, operandCount, out);
    }
    else
    {
        List<IRType*> argTypes;
        argTypes.setCount(operandCount);

        for (Index i = 0; i < operandCount; ++i)
        {
            auto operand = inst->getOperand(i);
            argTypes[i] = m_typeSet->getType(operand->getDataType());
        }
        _calcIntrinsic(op, returnType, argTypes.getBuffer(), operandCount, out);
    }
}

void HLSLIntrinsicSet::calcIntrinsic(HLSLIntrinsic::Op op, IRType* returnType, IRUse* inArgs, Index argCount, HLSLIntrinsic& out)
{
    returnType = m_typeSet->getType(returnType);

    if (argCount <= 8)
    {
        IRType* argTypes[8];

        for (Index i = 0; i < argCount; ++i)
        {
            auto operand = inArgs[i].get();
            argTypes[i] = m_typeSet->getType(operand->getDataType());
        }
        _calcIntrinsic(op, returnType, argTypes, argCount, out);
    }
    else
    {
        List<IRType*> argTypes;
        argTypes.setCount(argCount);

        for (Index i = 0; i < argCount; ++i)
        {
            auto operand = inArgs[i].get();
            argTypes[i] = m_typeSet->getType(operand->getDataType());
        }
        _calcIntrinsic(op, returnType, argTypes.getBuffer(), argCount, out);
    }
}

HLSLIntrinsic* HLSLIntrinsicSet::add(IRInst* inst)
{
    HLSLIntrinsic intrinsic;
    if (SLANG_SUCCEEDED(makeIntrinsic(inst, intrinsic)))
    {
        return add(intrinsic);
    }
    return nullptr;
}

SlangResult HLSLIntrinsicSet::makeIntrinsic(IRInst* inst, HLSLIntrinsic& out)
{
    // Mark as invalid... 
    out.op = Op::Invalid;

    {
        // See if we can just directly convert
        Op op = HLSLIntrinsicOpLookup::getOpForIROp(inst->getOp());


        // HACK: some cases we want to stop handling via the synthesis
        // path, but only for vector and matrix types (not scalars).
        //
        switch( op )
        {
        default: break;

        case Op::AsFloat:
        case Op::AsInt:
        case Op::AsUInt:
            // Note: the `any()`/`all()` case can't be handled via a stdlib definition
            // right now because `bool` vectors map to `int` vectors on the CUDA
            // path, so that the generated `geAt` operation is incorrect.
            //
//        case Op::Any:
//        case Op::All:
            {
                IRType* srcType = inst->getOperand(0)->getDataType();
                switch( srcType->getOp() )
                {
                default:
                    break;

                case kIROp_VectorType:
                case kIROp_MatrixType:
                    return SLANG_FAIL;
                }
            }
            break;
        }


        if (op != Op::Invalid)
        {
            calcIntrinsic(op, inst, inst->getOperandCount(), out);
            return SLANG_OK;
        }
    }

    // All the special cases
    switch (inst->getOp())
    {
        case kIROp_constructVectorFromScalar:
        {
            SLANG_ASSERT(inst->getOperandCount() == 1);
            calcIntrinsic(Op::ConstructFromScalar, inst, 1, out);
            return SLANG_OK;
        }
        case kIROp_Construct:
        {
            IRType* dstType = inst->getDataType();
            IRType* srcType = inst->getOperand(0)->getDataType();

            if ((dstType->getOp() == kIROp_VectorType || dstType->getOp() == kIROp_MatrixType) &&
                inst->getOperandCount() == 1)
            {
                if (as<IRBasicType>(srcType))
                {
                    calcIntrinsic(Op::ConstructFromScalar, inst, out);
                }
                else
                {
                    SLANG_ASSERT(m_typeSet->getType(dstType) != m_typeSet->getType(srcType));
                    // If it's constructed from a type conversion
                    calcIntrinsic(Op::ConstructConvert, inst, out);
                }
                return SLANG_OK;
            }
            else
            {
                // If we are constructing a basic type, we don't need an Op::Init
                if (!IRBasicType::isaImpl(dstType->getOp()))
                {
                    // Emit the 'init' intrinsic
                    calcIntrinsic(Op::Init, inst, inst->getOperandCount(), out);
                    return SLANG_OK;
                }
            }
            return SLANG_FAIL;
        }
        case kIROp_makeVector:
        {
            if (inst->getOperandCount() == 1 && as<IRBasicType>(inst->getOperand(0)->getDataType()))
            {
                // This is make from scalar
                calcIntrinsic(Op::ConstructFromScalar, inst, out);
            }
            else
            {
                calcIntrinsic(Op::Init, inst, inst->getOperandCount(), out);
            }
            return SLANG_OK;
        }
        case kIROp_MakeMatrix:
        {
            // We only emit as if it has one operand, but we can tell how many it actually has from the return type
            calcIntrinsic(Op::Init, inst, inst->getOperandCount(), out);
            return SLANG_OK;
        }
        case kIROp_swizzle:
        {
            // We don't need to add swizzle function, but we do output the need for some other functions 

            // For C++ we don't need to emit a swizzle function
            // For C we need a construction function
            auto swizzleInst = static_cast<IRSwizzle*>(inst);

            IRInst* baseInst = swizzleInst->getBase();
            IRType* baseType = baseInst->getDataType();

            // If we are swizzling from a built in type, 
            if (as<IRBasicType>(baseType))
            {
                // We can swizzle a scalar type to be a vector, or just a scalar
                IRType* dstType = swizzleInst->getDataType();
                if (!as<IRBasicType>(dstType))
                {
                    // If it's a scalar make sure we have construct from scalar, because we will want to use that
                    SLANG_ASSERT(dstType->getOp() == kIROp_VectorType);
                    IRType* argTypes[] = { baseType };
                    calcIntrinsic(Op::ConstructFromScalar, inst->getDataType(), argTypes, 1,  out);
                    return SLANG_OK;
                }
            }
            else
            {
                const Index elementCount = Index(swizzleInst->getElementCount());
                if (elementCount >= 1)
                {
                    // Will need to generate a swizzle method
                    calcIntrinsic(Op::Swizzle, inst, out);
                    return SLANG_OK;
                }
            }
            break;
        }
        case kIROp_getElement:
        {
            IRInst* target = inst->getOperand(0);
            IRType* targetType = target->getDataType();
            if (targetType->getOp() == kIROp_VectorType || targetType->getOp() == kIROp_MatrixType)
            {
                // Specially handle this
                calcIntrinsic(Op::GetAt, inst, out);
                return SLANG_OK;
            }
            break;
        }
        case kIROp_getElementPtr:
        {
            IRInst* target = inst->getOperand(0);
            IRType* targetType = target->getDataType();

            if (auto ptrType = as<IRPtrType>(targetType))
            {
                targetType = as<IRType>(ptrType->getOperand(0));
                if (targetType->getOp() == kIROp_VectorType || targetType->getOp() == kIROp_MatrixType)
                {
                    // Specially handle this
                    calcIntrinsic(Op::GetAt, inst, out);
                    return SLANG_OK;
                }
            }
            break;
        }
        case kIROp_Call:
        {
            IRCall* callInst = (IRCall*)inst;
            auto funcValue = callInst->getCallee();

            const Op op = m_opLookup->getOpFromTargetDecoration(funcValue);
            if (op != Op::Invalid)
            {
                calcIntrinsic(op, inst->getDataType(), callInst->getArgs(), callInst->getArgCount(), out);
                return SLANG_OK;
            }
            break;
        }

        default: break;
    }

    return SLANG_FAIL;
}

void HLSLIntrinsicSet::getIntrinsics(List<const HLSLIntrinsic*>& out) const
{
    for (auto& intrinsic : m_intrinsicsList)
    {
        out.add(intrinsic);
    }
}

HLSLIntrinsic* HLSLIntrinsicSet::add(const HLSLIntrinsic& intrinsic)
{
    // Make sure it's valid(!)
    SLANG_ASSERT(intrinsic.op != Op::Invalid);

    HLSLIntrinsic* copy = (HLSLIntrinsic*)m_intrinsicFreeList.allocate();
    *copy = intrinsic;
    HLSLIntrinsicRef ref(copy);
    HLSLIntrinsic** found =  m_intrinsicsDict.TryGetValueOrAdd(ref, copy);
    if (found)
    {
        // If we have found an intrinsic, we can free the copy
        m_intrinsicFreeList.deallocate(copy);
        return *found;
    }

    // If we are adding an intrinsic for the first time,
    // it should be added to the deduplicated list
    m_intrinsicsList.add(copy);

    return copy;
}

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!! HLSLIntrinsicOpLookup !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

HLSLIntrinsicOpLookup::HLSLIntrinsicOpLookup():
    m_slicePool(StringSlicePool::Style::Default)
{
    // Add all the operations with names (not ops like -, / etc) to the lookup map
    for (int i = 0; i < SLANG_COUNT_OF(HLSLIntrinsic::s_operationInfos); ++i)
    {
        const auto& info = HLSLIntrinsic::getInfo(Op(i));
        UnownedStringSlice slice = info.funcName;

        if (slice.getLength() > 0 && slice[0] >= 'a' && slice[0] <= 'z')
        {
            auto handle = m_slicePool.add(slice);
            Index index = Index(handle);
            // Make sure there is space
            if (index >= m_sliceToOpMap.getCount())
            {
                Index oldSize = m_sliceToOpMap.getCount();
                m_sliceToOpMap.setCount(index + 1);
                for (Index j = oldSize; j < index; j++)
                {
                    m_sliceToOpMap[j] = Op::Invalid;
                }
            }
            m_sliceToOpMap[index] = Op(i);
        }
    }
}

HLSLIntrinsic::Op HLSLIntrinsicOpLookup::getOpByName(const UnownedStringSlice& slice)
{
    const Index index = m_slicePool.findIndex(slice);
    return (index >= 0 && index < m_sliceToOpMap.getCount()) ? m_sliceToOpMap[index] : Op::Invalid;
}

static IRInst* _getSpecializedValue(IRSpecialize* specInst)
{
    auto base = specInst->getBase();
    auto baseGeneric = as<IRGeneric>(base);
    if (!baseGeneric)
        return base;

    auto lastBlock = baseGeneric->getLastBlock();
    if (!lastBlock)
        return base;

    auto returnInst = as<IRReturnVal>(lastBlock->getTerminator());
    if (!returnInst)
        return base;

    return returnInst->getVal();
}

HLSLIntrinsic::Op HLSLIntrinsicOpLookup::getOpFromTargetDecoration(IRInst* inInst)
{
    // An intrinsic generic function will be invoked through a `specialize` instruction,
    // so the callee won't directly be the thing that is decorated. We will look up
    // through specializations until we can see the actual thing being called.
    //
    IRInst* inst = inInst;
    while (auto specInst = as<IRSpecialize>(inst))
    {
        inst = _getSpecializedValue(specInst);

        // If `getSpecializedValue` can't find the result value
        // of the generic being specialized, then it returns
        // the original instruction. This would be a disaster
        // for use because this loop would go on forever.
        //
        // This case should never happen if the stdlib is well-formed
        // and the compiler is doing its job right.
        //
        SLANG_ASSERT(inst != specInst);
    }

    // We are just looking for the original name so we can match against it
    for (auto dd : inst->getDecorations())
    {
        if (auto decor = as<IRTargetIntrinsicDecoration>(dd))
        {
            // TODO(JS): Should confirm that we'll always have this entry - which we need for lookups to work (we need the name
            // not a targets transformation)
            // 
            // It turns out that addCatchAllIntrinsicDecorationIfNeeded will add a target intrinsic with the
            // original HLSL name, which has an empty `CapabilitySet`.
            // 
            // It's not 100% clear this covers all the cases, but for now lets go with that
            if (decor->getTargetCaps().isEmpty())
            {
                Op op = getOpByName(decor->getDefinition());
                if (op != Op::Invalid)
                {
                    return op;
                }
            }
        }
    }

    return Op::Invalid;
}

HLSLIntrinsic::Op HLSLIntrinsicOpLookup::getOpForIROp(IRInst* inst)
{
    switch (inst->getOp())
    {
        case kIROp_Call:
        {
            return getOpFromTargetDecoration(inst);
        }
        default: break;
    }
    return getOpForIROp(inst->getOp());
}

/* static */HLSLIntrinsic::Op HLSLIntrinsicOpLookup::getOpForIROp(IROp op)
{
    switch (op)
    {
        case kIROp_Add:     return Op::Add;
        case kIROp_Mul:     return Op::Mul;
        case kIROp_Sub:     return Op::Sub;
        case kIROp_Div:     return Op::Div;
        case kIROp_Lsh:     return Op::Lsh;
        case kIROp_Rsh:     return Op::Rsh;
        case kIROp_IRem:    return Op::IRem;
        case kIROp_FRem:    return Op::FRem;

        case kIROp_Eql:     return Op::Eql;
        case kIROp_Neq:     return Op::Neq;
        case kIROp_Greater: return Op::Greater;
        case kIROp_Less:    return Op::Less;
        case kIROp_Geq:     return Op::Geq;
        case kIROp_Leq:     return Op::Leq;

        case kIROp_BitAnd:  return Op::BitAnd;
        case kIROp_BitXor:  return Op::BitXor;
        case kIROp_BitOr:   return Op::BitOr;

        case kIROp_And:     return Op::And;
        case kIROp_Or:      return Op::Or;

        case kIROp_Neg:     return Op::Neg;
        case kIROp_Not:     return Op::Not;
        case kIROp_BitNot:  return Op::BitNot;

        case kIROp_constructVectorFromScalar: return Op::ConstructFromScalar;

        default:            return Op::Invalid;
    }
}

}