summaryrefslogtreecommitdiff
path: root/source/slang/slang-ir-layout.cpp
blob: 0003d279a4f99314ef6e0deecd78f965797380d5 (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
// slang-ir-layout.cpp
#include "slang-ir-layout.h"

#include "slang-ir-insts.h"

// This file implements facilities for computing and caching layout
// information on IR types.
//
// Unlike the AST-level layout system, this code currently only
// handles the notion of "natural" layout for IR types, which is
// the layout they use when stored in general-purpose memory
// without additional constraints.
//
// In general, "natural" layout for all targets is assumed to follow
// the same basic rules:
//
// * Scalars are all naturally aligned and have the "obvious" size
//
// * Arrays are laid out by separating elements by their "stride" (size rounded up to alignment)
//
// * Vectors are laid out as arrays of elements
//
// * Matrices are laid out as arrays of rows
//
// * Structures are laid out by packing fields in order, placing each field on the "next"
//   suitably aligned offset. The alignment of a structure is the maximum alignment of
//   its fields.
//
// Right now this file implements a one-size-fits-all version of natural
// layout that might not be a perfect fit for all targets. In particular
// this code currently assumes:
//
// * The `bool` type is laid out as 4 bytes (equivalent to an `int`)
//
// * The size of a structure or array type is *not* rounded up to a multiple
//   of its alignment. This means that fields may be laid out in
//   the "tail padding" of previous fields in the same structure. This is
//   correct behavior for VK/D3D, but does not match the behavior of typical
//   C/C++ compilers.
//
// * All matrices are laid out in row-major order, regardless of any
//   settings in user code.
//
// TODO: Addressing the above issues would require extending this file to somehow
// get target-specific layout information as an input. One option would be
// to attach information about "natural" layout on the target to the `IRModuleInst`
// as a decoration, similar to how an LLVM IR module stores a "layout string."

namespace Slang
{

static Result _calcNaturalSizeAndAlignment(IRType* type, IRSizeAndAlignment* outSizeAndAlignment)
{
    switch( type->op )
    {

#define CASE(TYPE, SIZE, ALIGNMENT)                                 \
    case kIROp_##TYPE##Type:                                        \
        *outSizeAndAlignment = IRSizeAndAlignment(SIZE, ALIGNMENT); \
        return SLANG_OK                                             \
        /* end */

    // Most base types are "naturally aligned" (meaning alignment and size are the same)
#define BASE(TYPE, SIZE) CASE(TYPE, SIZE, SIZE)

    BASE(Int8,      1);
    BASE(UInt8,     1);

    BASE(Int16,     2);
    BASE(UInt16,    2);
    BASE(Half,      2);

    BASE(Int,       4);
    BASE(UInt,      4);
    BASE(Float,     4);

    BASE(Int64,     8);
    BASE(UInt64,    8);
    BASE(Double,    8);

    // We are currently handling `bool` following the HLSL
    // precednet of storing it in 4 bytes.
    //
    // TODO: It would be good to try to make this follow
    // per-platform conventions, or at least to be able
    // to use a 1-byte encoding where available.
    //
    BASE(Bool,      4);

    // The Slang `void` type is treated as a zero-byte
    // type, so that it does not influence layout at all.
    //
    CASE(Void,      0,  1);

#undef CASE

#undef CASE

    case kIROp_StructType:
        {
            auto structType = cast<IRStructType>(type);
            IRSizeAndAlignment structLayout;
            for( auto field : structType->getFields() )
            {
                IRSizeAndAlignment fieldTypeLayout;
                SLANG_RETURN_ON_FAIL(getNaturalSizeAndAlignment(field->getFieldType(), &fieldTypeLayout));

                structLayout.size = align(structLayout.size, fieldTypeLayout.alignment);
                structLayout.alignment = std::max(structLayout.alignment, fieldTypeLayout.alignment);

                IRIntegerValue fieldOffset = structLayout.size;
                if( auto module = type->getModule() )
                {
                    // If we are in a situation where attaching new
                    // decorations is possible, then we want to
                    // cache the field offset on the IR field
                    // instruction.
                    //
                    SharedIRBuilder sharedBuilder;
                    sharedBuilder.module = module;
                    sharedBuilder.session = module->getSession();

                    IRBuilder builder;
                    builder.sharedBuilder = &sharedBuilder;

                    auto intType = builder.getIntType();
                    builder.addDecoration(
                        field,
                        kIROp_NaturalOffsetDecoration,
                        builder.getIntValue(intType, fieldOffset));
                }

                structLayout.size += fieldTypeLayout.size;
            }
            *outSizeAndAlignment = structLayout;
            return SLANG_OK;
        }
        break;

    case kIROp_ArrayType:
        {
            auto arrayType = cast<IRArrayType>(type);

            auto elementCountLit = as<IRIntLit>(arrayType->getElementCount());
            if(!elementCountLit)
                return SLANG_FAIL;
            auto elementCount = elementCountLit->getValue();

            if( elementCount == 0 )
            {
                *outSizeAndAlignment = IRSizeAndAlignment(0, 1);
                return SLANG_OK;
            }

            auto elementType = arrayType->getElementType();
            IRSizeAndAlignment elementTypeLayout;
            SLANG_RETURN_ON_FAIL(getNaturalSizeAndAlignment(elementType, &elementTypeLayout));

            auto elementStride = elementTypeLayout.getStride();

            *outSizeAndAlignment = IRSizeAndAlignment(
                elementStride * (elementCount - 1) + elementTypeLayout.size,
                elementTypeLayout.alignment);
            return SLANG_OK;
        }
        break;

    default:
        return SLANG_FAIL;
    }
}

Result getNaturalSizeAndAlignment(IRType* type, IRSizeAndAlignment* outSizeAndAlignment)
{
    if( auto decor = type->findDecoration<IRNaturalSizeAndAlignmentDecoration>() )
    {
        *outSizeAndAlignment = IRSizeAndAlignment(decor->getSize(), (int)decor->getAlignment());
        return SLANG_OK;
    }

    IRSizeAndAlignment sizeAndAlignment;
    SLANG_RETURN_ON_FAIL(_calcNaturalSizeAndAlignment(type, &sizeAndAlignment));

    if( auto module = type->getModule() )
    {
        SharedIRBuilder sharedBuilder;
        sharedBuilder.module = module;
        sharedBuilder.session = module->getSession();

        IRBuilder builder;
        builder.sharedBuilder = &sharedBuilder;

        auto intType = builder.getIntType();
        builder.addDecoration(
            type,
            kIROp_NaturalSizeAndAlignmentDecoration,
            builder.getIntValue(intType, sizeAndAlignment.size),
            builder.getIntValue(intType, sizeAndAlignment.alignment));
    }

    *outSizeAndAlignment = sizeAndAlignment;
    return SLANG_OK;
}


Result getNaturalOffset(IRStructField* field, IRIntegerValue* outOffset)
{
    if( auto decor = field->findDecoration<IRNaturalOffsetDecoration>() )
    {
        *outOffset = decor->getOffset();
        return SLANG_OK;
    }

    // Offsets are computed as part of layout out types,
    // so we expect that layout of the "parent" type
    // of the field should add an offset to it if
    // possible.

    auto structType = as<IRStructType>(field->getParent());
    if(!structType)
        return SLANG_FAIL;

    IRSizeAndAlignment structTypeLayout;
    SLANG_RETURN_ON_FAIL(getNaturalSizeAndAlignment(structType, &structTypeLayout));

    if( auto decor = field->findDecoration<IRNaturalOffsetDecoration>() )
    {
        *outOffset = decor->getOffset();
        return SLANG_OK;
    }

    // If attempting to lay out the parent type didn't
    // cause the field to get an offset, then we are
    // in an unexpected case with no easy answer.
    //
    return SLANG_FAIL;
}

}