File: CacheControlsHelper.h

package info (click to toggle)
intel-graphics-compiler2 2.28.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 792,744 kB
  • sloc: cpp: 5,761,745; ansic: 466,928; lisp: 312,143; python: 114,790; asm: 44,736; pascal: 10,930; sh: 8,033; perl: 7,914; ml: 3,625; awk: 3,523; yacc: 2,747; javascript: 2,667; lex: 1,898; f90: 1,028; cs: 573; xml: 474; makefile: 344; objc: 162
file content (413 lines) | stat: -rw-r--r-- 19,853 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2023 Intel Corporation

SPDX-License-Identifier: MIT

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

#pragma once

#include "Compiler/CISACodeGen/getCacheOpts.h"

namespace IGC {
constexpr uint64_t DecorationIdCacheControlLoad = 6442;
constexpr uint64_t DecorationIdCacheControlStore = 6443;

enum class LoadCacheControl {
  Uncached = 0,
  Cached = 1,
  Streaming = 2,
  InvalidateAfterRead = 3,
  ConstCached = 4,
  Invalid // This value represents invalid/unsupported cache control value
};

enum class StoreCacheControl {
  Uncached = 0,
  WriteThrough = 1,
  WriteBack = 2,
  Streaming = 3,
  Invalid // This value represents invalid/unsupported cache control value
};

template <typename T> inline uint64_t getDecorationIdCacheControl() {
  static_assert(std::is_same_v<T, LoadCacheControl> || std::is_same_v<T, StoreCacheControl>);
  return (std::is_same_v<T, LoadCacheControl> ? DecorationIdCacheControlLoad : DecorationIdCacheControlStore);
}

template <typename T> inline int getDefaultCacheControlValue(CodeGenContext *ctx) {
  static_assert(std::is_same_v<T, LoadCacheControl> || std::is_same_v<T, StoreCacheControl>);
  return std::is_same_v<T, LoadCacheControl> ? ctx->getModuleMetaData()->compOpt.LoadCacheDefault
                                             : ctx->getModuleMetaData()->compOpt.StoreCacheDefault;
}

template <typename T> struct SeparateCacheControlsL1L3 {
  T L1;
  T L3;
};

template <typename T>
using CacheControlMapTy = std::unordered_map<LSC_L1_L3_CC, SeparateCacheControlsL1L3<T>, std::hash<int>>;

const CacheControlMapTy<StoreCacheControl> supportedStoreConfigs = {
    // clang-format off
        { LSC_L1UC_L3UC,       { StoreCacheControl::Uncached,     StoreCacheControl::Uncached  } },
        { LSC_L1UC_L3C_WB,     { StoreCacheControl::Uncached,     StoreCacheControl::WriteBack } },
        { LSC_L1C_WT_L3UC,     { StoreCacheControl::WriteThrough, StoreCacheControl::Uncached  } },
        { LSC_L1C_WT_L3C_WB,   { StoreCacheControl::WriteThrough, StoreCacheControl::WriteBack } },
        { LSC_L1S_L3UC,        { StoreCacheControl::Streaming,    StoreCacheControl::Uncached  } },
        { LSC_L1S_L3C_WB,      { StoreCacheControl::Streaming,    StoreCacheControl::WriteBack } },
        { LSC_L1IAR_WB_L3C_WB, { StoreCacheControl::WriteBack,    StoreCacheControl::WriteBack } }
    // clang-format on
};

const CacheControlMapTy<LoadCacheControl> supportedLoadConfigs = {
    // clang-format off
        { LSC_L1UC_L3UC,       { LoadCacheControl::Uncached,            LoadCacheControl::Uncached } },
        { LSC_L1UC_L3C_WB,     { LoadCacheControl::Uncached,            LoadCacheControl::Cached   } },
        { LSC_L1C_WT_L3UC,     { LoadCacheControl::Cached,              LoadCacheControl::Uncached } },
        { LSC_L1C_WT_L3C_WB,   { LoadCacheControl::Cached,              LoadCacheControl::Cached   } },
        { LSC_L1S_L3UC,        { LoadCacheControl::Streaming,           LoadCacheControl::Uncached } },
        { LSC_L1S_L3C_WB,      { LoadCacheControl::Streaming,           LoadCacheControl::Cached   } },
        { LSC_L1IAR_WB_L3C_WB, { LoadCacheControl::InvalidateAfterRead, LoadCacheControl::Cached   } },
        { LSC_L1UC_L3CC,       { LoadCacheControl::Uncached,            LoadCacheControl::ConstCached,        } },
        { LSC_L1C_L3CC,        { LoadCacheControl::Cached,              LoadCacheControl::ConstCached,        } },
        { LSC_L1IAR_L3IAR,     { LoadCacheControl::InvalidateAfterRead, LoadCacheControl::InvalidateAfterRead } },
    // clang-format on
};
template <typename T> struct SeparateCacheControlsL1L2L3 {
  T L1;
  T L2;
  T L3;
};

template <typename T>
using L1L2L3StoreCacheControlMapTy =
    std::unordered_map<LSC_STCC_L1_L2_L3, SeparateCacheControlsL1L2L3<T>, std::hash<int>>;
const L1L2L3StoreCacheControlMapTy<StoreCacheControl> supportedL1L2L3StoreConfigs = {
    // clang-format off
        { LSC_STCC_L1UC_L2UC_L3UC, { StoreCacheControl::Uncached,     StoreCacheControl::Uncached,  StoreCacheControl::Uncached } },
        { LSC_STCC_L1UC_L2UC_L3WB, { StoreCacheControl::Uncached,     StoreCacheControl::Uncached,  StoreCacheControl::WriteBack } },
        { LSC_STCC_L1UC_L2WB_L3UC, { StoreCacheControl::Uncached,     StoreCacheControl::WriteBack, StoreCacheControl::Uncached } },
        { LSC_STCC_L1UC_L2WB_L3WB, { StoreCacheControl::Uncached,     StoreCacheControl::WriteBack, StoreCacheControl::WriteBack } },
        { LSC_STCC_L1WT_L2UC_L3UC, { StoreCacheControl::WriteThrough, StoreCacheControl::Uncached,  StoreCacheControl::Uncached } },
        { LSC_STCC_L1WT_L2UC_L3WB, { StoreCacheControl::WriteThrough, StoreCacheControl::Uncached,  StoreCacheControl::WriteBack } },
        { LSC_STCC_L1WT_L2WB_L3UC, { StoreCacheControl::WriteThrough, StoreCacheControl::WriteBack, StoreCacheControl::Uncached } },
        { LSC_STCC_L1WT_L2WB_L3WB, { StoreCacheControl::WriteThrough, StoreCacheControl::WriteBack, StoreCacheControl::WriteBack } },
        { LSC_STCC_L1S_L2UC_L3UC,  { StoreCacheControl::Streaming,    StoreCacheControl::Uncached,  StoreCacheControl::Uncached } },
        { LSC_STCC_L1S_L2UC_L3WB,  { StoreCacheControl::Streaming,    StoreCacheControl::Uncached,  StoreCacheControl::WriteBack } },
        { LSC_STCC_L1S_L2WB_L3UC,  { StoreCacheControl::Streaming,    StoreCacheControl::WriteBack, StoreCacheControl::Uncached } },
        { LSC_STCC_L1WB_L2UC_L3UC, { StoreCacheControl::WriteBack,    StoreCacheControl::Uncached,  StoreCacheControl::Uncached } },
        { LSC_STCC_L1WB_L2WB_L3UC, { StoreCacheControl::WriteBack,    StoreCacheControl::WriteBack, StoreCacheControl::Uncached } },
        { LSC_STCC_L1WB_L2UC_L3WB, { StoreCacheControl::WriteBack,    StoreCacheControl::Uncached,  StoreCacheControl::WriteBack } },
    // clang-format on
};

template <typename T>
using L1L2L3LoadCacheControlMapTy =
    std::unordered_map<LSC_LDCC_L1_L2_L3, SeparateCacheControlsL1L2L3<T>, std::hash<int>>;
const L1L2L3LoadCacheControlMapTy<LoadCacheControl> supportedL1L2L3LoadConfigs = {
    // clang-format off
        { LSC_LDCC_L1UC_L2UC_L3UC,    { LoadCacheControl::Uncached,  LoadCacheControl::Uncached, LoadCacheControl::Uncached } },
        { LSC_LDCC_L1UC_L2UC_L3C,     { LoadCacheControl::Uncached,  LoadCacheControl::Uncached, LoadCacheControl::Cached } },
        { LSC_LDCC_L1UC_L2C_L3UC,     { LoadCacheControl::Uncached,  LoadCacheControl::Cached,   LoadCacheControl::Uncached } },
        { LSC_LDCC_L1UC_L2C_L3C,      { LoadCacheControl::Uncached,  LoadCacheControl::Cached,   LoadCacheControl::Cached } },
        { LSC_LDCC_L1C_L2UC_L3UC,     { LoadCacheControl::Cached,    LoadCacheControl::Uncached, LoadCacheControl::Uncached } },
        { LSC_LDCC_L1C_L2UC_L3C,      { LoadCacheControl::Cached,    LoadCacheControl::Uncached, LoadCacheControl::Cached } },
        { LSC_LDCC_L1C_L2C_L3UC,      { LoadCacheControl::Cached,    LoadCacheControl::Cached,   LoadCacheControl::Uncached } },
        { LSC_LDCC_L1C_L2C_L3C,       { LoadCacheControl::Cached,    LoadCacheControl::Cached,   LoadCacheControl::Cached } },
        { LSC_LDCC_L1S_L2UC_L3UC,     { LoadCacheControl::Streaming, LoadCacheControl::Uncached, LoadCacheControl::Uncached } },
        { LSC_LDCC_L1S_L2UC_L3C,      { LoadCacheControl::Streaming, LoadCacheControl::Uncached, LoadCacheControl::Cached } },
        { LSC_LDCC_L1S_L2C_L3UC,      { LoadCacheControl::Streaming, LoadCacheControl::Cached,   LoadCacheControl::Uncached } },
        { LSC_LDCC_L1S_L2C_L3C,       { LoadCacheControl::Streaming, LoadCacheControl::Cached,   LoadCacheControl::Cached } },
        { LSC_LDCC_L1IAR_L2IAR_L3IAR, { LoadCacheControl::InvalidateAfterRead, LoadCacheControl::InvalidateAfterRead, LoadCacheControl::InvalidateAfterRead } },
    // clang-format on
};

using CacheLevel = uint64_t;

// This function expects MDNodes to be in the following form:
// MD =  {  opcode,   cache_level,  cache_control}
// -----------------------------------------------
// !8 =  !{i32 6442,    i32 1,          i32 0   }
// !9 =  !{i32 6442,    i32 3,          i32 0   }
// !11 = !{i32 6442,    i32 1,          i32 0   }
// !12 = !{i32 6442,    i32 3,          i32 1   }
template <typename T>
inline llvm::SmallDenseMap<CacheLevel, T> parseCacheControlsMD(llvm::SmallPtrSetImpl<llvm::MDNode *> &MDNodes) {
  using namespace llvm;
  auto getLiteral = [](const MDNode *node, const uint32_t index) {
    if (auto value = dyn_cast<ValueAsMetadata>(node->getOperand(index)))
      if (auto constantInt = dyn_cast<ConstantInt>(value->getValue()))
        return std::optional<uint64_t>(constantInt->getZExtValue());

    return std::optional<uint64_t>();
  };

  SmallDenseMap<CacheLevel, T> cacheControls;
  for (auto Node : MDNodes) {
    IGC_ASSERT(Node->getNumOperands() == 3);
    IGC_ASSERT(getLiteral(Node, 0).value() == DecorationIdCacheControlLoad ||
               getLiteral(Node, 0).value() == DecorationIdCacheControlStore);

    auto cacheLevel = getLiteral(Node, 1);
    auto cacheControl = getLiteral(Node, 2);

    IGC_ASSERT(cacheLevel && cacheControl);
    cacheControls[cacheLevel.value()] = static_cast<T>(cacheControl.value());
  }
  return cacheControls;
}

template <typename T>
inline std::optional<T> getCacheControl(llvm::SmallDenseMap<CacheLevel, T> &cacheControls, CacheLevel level) {
  if (auto E = cacheControls.find(level); E != cacheControls.end())
    return E->second;
  return {};
}

inline LSC_L1_L3_CC mapToLSCCacheControl(StoreCacheControl L1Control, StoreCacheControl L3Control) {
  for (auto &[LSCEnum, SPIRVEnum] : supportedStoreConfigs)
    if (SPIRVEnum.L1 == L1Control && SPIRVEnum.L3 == L3Control)
      return LSCEnum;

  return LSC_CC_INVALID;
}

inline LSC_L1_L3_CC mapToLSCCacheControl(LoadCacheControl L1Control, LoadCacheControl L3Control) {
  for (auto &[LSCEnum, SPIRVEnum] : supportedLoadConfigs)
    if (SPIRVEnum.L1 == L1Control && SPIRVEnum.L3 == L3Control)
      return LSCEnum;

  return LSC_CC_INVALID;
}

inline LSC_STCC_L1_L2_L3 mapToLSCL1L2L3CacheControl(StoreCacheControl L1Control, StoreCacheControl L2Control,
                                                    StoreCacheControl L3Control) {
  for (auto &[LSCEnum, SPIRVEnum] : supportedL1L2L3StoreConfigs)
    if (SPIRVEnum.L1 == L1Control && SPIRVEnum.L2 == L2Control && SPIRVEnum.L3 == L3Control)
      return LSCEnum;

  return LSC_STCC_INVALID;
}

inline LSC_LDCC_L1_L2_L3 mapToLSCL1L2L3CacheControl(LoadCacheControl L1Control, LoadCacheControl L2Control,
                                                    LoadCacheControl L3Control) {
  for (auto &[LSCEnum, SPIRVEnum] : supportedL1L2L3LoadConfigs)
    if (SPIRVEnum.L1 == L1Control && SPIRVEnum.L2 == L2Control && SPIRVEnum.L3 == L3Control)
      return LSCEnum;

  return LSC_LDCC_INVALID;
}

template <typename T> SeparateCacheControlsL1L3<T> mapToSPIRVCacheControl(LSC_L1_L3_CC) = delete;

template <>
inline SeparateCacheControlsL1L3<LoadCacheControl> mapToSPIRVCacheControl<LoadCacheControl>(LSC_L1_L3_CC LSCControl) {
  if (auto I = supportedLoadConfigs.find(LSCControl); I != supportedLoadConfigs.end())
    return I->second;

  IGC_ASSERT_MESSAGE(false, "Unsupported cache controls combination!");
  return {LoadCacheControl::Invalid, LoadCacheControl::Invalid};
}

template <>
inline SeparateCacheControlsL1L3<StoreCacheControl> mapToSPIRVCacheControl<StoreCacheControl>(LSC_L1_L3_CC LSCControl) {
  if (auto I = supportedStoreConfigs.find(LSCControl); I != supportedStoreConfigs.end())
    return I->second;

  IGC_ASSERT_MESSAGE(false, "Unsupported cache controls combination!");
  return {StoreCacheControl::Invalid, StoreCacheControl::Invalid};
}

template <typename T> SeparateCacheControlsL1L2L3<T> mapToSPIRVL1L2L3CacheControl(int) = delete;

template <>
inline SeparateCacheControlsL1L2L3<LoadCacheControl> mapToSPIRVL1L2L3CacheControl<LoadCacheControl>(int cacheControl) {
  auto LSCLoadCacheControl = static_cast<LSC_LDCC_L1_L2_L3>(cacheControl);
  if (auto I = supportedL1L2L3LoadConfigs.find(LSCLoadCacheControl); I != supportedL1L2L3LoadConfigs.end())
    return I->second;

  IGC_ASSERT_MESSAGE(false, "Unsupported cache controls combination!");
  return {LoadCacheControl::Invalid, LoadCacheControl::Invalid, LoadCacheControl::Invalid};
}

template <>
inline SeparateCacheControlsL1L2L3<StoreCacheControl>
mapToSPIRVL1L2L3CacheControl<StoreCacheControl>(int cacheControl) {
  auto LSCStoreCacheControl = static_cast<LSC_STCC_L1_L2_L3>(cacheControl);
  if (auto I = supportedL1L2L3StoreConfigs.find(LSCStoreCacheControl); I != supportedL1L2L3StoreConfigs.end())
    return I->second;

  IGC_ASSERT_MESSAGE(false, "Unsupported cache controls combination!");
  return {StoreCacheControl::Invalid, StoreCacheControl::Invalid, StoreCacheControl::Invalid};
}

inline llvm::DenseMap<uint64_t, llvm::SmallPtrSet<llvm::MDNode *, 4>> parseSPIRVDecorationsFromMD(llvm::Value *V) {
  using namespace llvm;
  MDNode *spirvDecorationsMD = nullptr;
  if (auto *GV = dyn_cast<GlobalVariable>(V)) {
    spirvDecorationsMD = GV->getMetadata("spirv.Decorations");
  } else if (auto *II = dyn_cast<Instruction>(V)) {
    spirvDecorationsMD = II->getMetadata("spirv.Decorations");
  } else if (auto *A = dyn_cast<Argument>(V)) {
    Function *F = A->getParent();
    auto *parameterMD = F->getMetadata("spirv.ParameterDecorations");

    if (parameterMD) {
      spirvDecorationsMD = cast<MDNode>(parameterMD->getOperand(A->getArgNo()));
    }
  }

  DenseMap<uint64_t, SmallPtrSet<MDNode *, 4>> spirvDecorations;
  if (spirvDecorationsMD) {
    for (const auto &operand : spirvDecorationsMD->operands()) {
      auto node = dyn_cast<MDNode>(operand.get());
      if (node->getNumOperands() == 0) {
        continue;
      }

      if (auto value = dyn_cast<ValueAsMetadata>(node->getOperand(0))) {
        if (auto constantInt = dyn_cast<ConstantInt>(value->getValue())) {
          uint64_t decorationId = constantInt->getZExtValue();

          spirvDecorations[decorationId].insert(node);
        }
      }
    }
  }
  return spirvDecorations;
}

struct CacheControlFromMDNodes {
  int value; // equal to default if (isEmpty || isInvalid)
  bool isEmpty;
  bool isInvalid;
};
template <typename T>
CacheControlFromMDNodes resolveCacheControlFromMDNodes(CodeGenContext *ctx,
                                                       llvm::SmallPtrSetImpl<llvm::MDNode *> &MDNodes) {
  using namespace llvm;
  static_assert(std::is_same_v<T, LoadCacheControl> || std::is_same_v<T, StoreCacheControl>);
  SmallDenseMap<CacheLevel, T> cacheControls = parseCacheControlsMD<T>(MDNodes);
  IGC_ASSERT(!cacheControls.empty());

  // SPV_INTEL_cache_controls extension specification states the following:
  // "Cache Level is an unsigned 32-bit integer telling the cache level to
  //  which the control applies. The value 0 indicates the cache level closest
  //  to the processing unit, the value 1 indicates the next furthest cache
  //  level, etc. If some cache level does not exist, the decoration is ignored."
  //
  // Therefore Cache Level equal to 0 maps to L1$ and Cache Level equal to 1 maps to L3$.
  // Other Cache Level values are ignored.
  const int cacheDefault = getDefaultCacheControlValue<T>(ctx);

  CacheControlFromMDNodes result = {};
  result.value = cacheDefault;

  auto L1CacheControl = getCacheControl(cacheControls, CacheLevel(0));
  auto L3CacheControl = getCacheControl(cacheControls, CacheLevel(1));
  std::optional<T> L2CacheControl = {};
  auto nextCacheControl = getCacheControl(cacheControls, CacheLevel(2));
  if (nextCacheControl) {
    L2CacheControl = L3CacheControl;
    L3CacheControl = nextCacheControl;
  }

  if (!L1CacheControl && !L2CacheControl && !L3CacheControl) {
    // Early exit if there are no cache controls set for cache levels that are controllable
    // by Intel GPUs.
    result.isEmpty = true;
    return result;
  }

  if (ctx->platform.hasNewLSCCacheEncoding() && L1CacheControl && L3CacheControl &&
      (L2CacheControl || cacheDefault > LSC_CC_INVALID)) {
    if (!L2CacheControl) {
      // Case when only cacheDefault from compOpt has new enum encoding value.
      // Workaround to have L1 L3 settings in L1/L2CacheControl.
      L2CacheControl = L3CacheControl;
      L3CacheControl = {};
    }

    SeparateCacheControlsL1L2L3<T> L1L2L3Default;
    if (cacheDefault > LSC_CC_INVALID) {
      L1L2L3Default = mapToSPIRVL1L2L3CacheControl<T>(cacheDefault);
    } else {
      // Parse old legacy L1 L3 default settings
      LSC_L1_L3_CC defaultLSCCacheControls = static_cast<LSC_L1_L3_CC>(cacheDefault);
      auto L1L3DefaultLegacy = mapToSPIRVCacheControl<T>(defaultLSCCacheControls);
      L1L2L3Default.L1 = L1L3DefaultLegacy.L1;
      L1L2L3Default.L2 = L1L3DefaultLegacy.L3;
      L1L2L3Default.L3 = T::Uncached;
      if (std::is_same_v<T, LoadCacheControl>) {
        // Const cached not available, but it corresponds to
        // L2=cached L3=cached in new cache encoding.
        if (L1L2L3Default.L2 == static_cast<T>(LoadCacheControl::ConstCached)) {
          L1L2L3Default.L2 = static_cast<T>(LoadCacheControl::Cached);
          L1L2L3Default.L3 = L1L2L3Default.L2;
        }

        // Only IAR, IAR, IAR allowed.
        if (L1L2L3Default.L2 == static_cast<T>(LoadCacheControl::InvalidateAfterRead)) {
          L1L2L3Default.L3 = L1L2L3Default.L2;
        }
      }
    }
    IGC_ASSERT(L1L2L3Default.L1 != T::Invalid && L1L2L3Default.L2 != T::Invalid && L1L2L3Default.L3 != T::Invalid);

    T newL1CacheControl = L1CacheControl ? L1CacheControl.value() : L1L2L3Default.L1;
    T newL2CacheControl = L2CacheControl ? L2CacheControl.value() : L1L2L3Default.L2;
    T newL3CacheControl = L3CacheControl ? L3CacheControl.value() : L1L2L3Default.L3;

    if (std::is_same_v<T, LoadCacheControl>) {
      // Const cached not available, but it corresponds to
      // L2=cached L3=cached in new cache encoding.
      if (newL2CacheControl == static_cast<T>(LoadCacheControl::ConstCached)) {
        newL2CacheControl = static_cast<T>(LoadCacheControl::Cached);
        newL3CacheControl = newL2CacheControl;
      }

      // Only IAR, IAR, IAR allowed.
      if (newL2CacheControl == static_cast<T>(LoadCacheControl::InvalidateAfterRead)) {
        newL3CacheControl = newL2CacheControl;
      }
    } else {
      // StoreCacheControl
      // Only for 2-level decorations, otherwise we would hide incorrect settings.
      if (!L3CacheControl && newL2CacheControl == static_cast<T>(StoreCacheControl::WriteBack) &&
          (newL1CacheControl == T::Streaming || newL1CacheControl == static_cast<T>(StoreCacheControl::WriteBack))) {
        // When L1 store cache is streaming or writeback,
        // writeback is not available for both L2 and L3 at the same time.
        newL3CacheControl = T::Uncached;
      }
    }

    auto newLSCCacheControl = mapToLSCL1L2L3CacheControl(newL1CacheControl, newL2CacheControl, newL3CacheControl);

    if (newLSCCacheControl == LSC_CC_INVALID) {
      result.isInvalid = true;
      return result;
    }
    result.value = newLSCCacheControl;
    return result;
  }

  LSC_L1_L3_CC defaultLSCCacheControls = static_cast<LSC_L1_L3_CC>(cacheDefault);
  auto L1L3Default = mapToSPIRVCacheControl<T>(defaultLSCCacheControls);
  IGC_ASSERT(L1L3Default.L1 != T::Invalid && L1L3Default.L3 != T::Invalid);

  T newL1CacheControl = L1CacheControl ? L1CacheControl.value() : L1L3Default.L1;
  T newL3CacheControl = L3CacheControl ? L3CacheControl.value() : L1L3Default.L3;

  LSC_L1_L3_CC newLSCCacheControl = mapToLSCCacheControl(newL1CacheControl, newL3CacheControl);

  if (newLSCCacheControl == LSC_CC_INVALID) {
    result.isInvalid = true;
    return result;
  }
  result.value = newLSCCacheControl;
  return result;
}
} // namespace IGC