forked from llvm/clangir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLowerToLLVM.cpp
4701 lines (4199 loc) · 190 KB
/
LowerToLLVM.cpp
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//====- LowerToLLVM.cpp - Lowering from CIR to LLVMIR ---------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements lowering of CIR operations to LLVMIR.
//
//===----------------------------------------------------------------------===//
#include "LowerToLLVM.h"
#include "LoweringHelpers.h"
#include "mlir/Conversion/AffineToStandard/AffineToStandard.h"
#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h"
#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
#include "mlir/Dialect/DLTI/DLTI.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/LLVMIR/Transforms/Passes.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinAttributeInterfaces.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinDialect.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/OpDefinition.h"
#include "mlir/IR/Operation.h"
#include "mlir/IR/Types.h"
#include "mlir/IR/Value.h"
#include "mlir/IR/ValueRange.h"
#include "mlir/IR/Visitors.h"
#include "mlir/Interfaces/DataLayoutInterfaces.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Support/LogicalResult.h"
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Export.h"
#include "clang/CIR/Dialect/IR/CIRAttrVisitor.h"
#include "clang/CIR/Dialect/Passes.h"
#include "clang/CIR/LoweringHelpers.h"
#include "clang/CIR/MissingFeatures.h"
#include "clang/CIR/Passes.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/TimeProfiler.h"
#include <cstdint>
#include <deque>
#include <optional>
#include <set>
using namespace cir;
using namespace llvm;
namespace cir {
namespace direct {
//===----------------------------------------------------------------------===//
// Helper Methods
//===----------------------------------------------------------------------===//
namespace {
/// Walks a region while skipping operations of type `Ops`. This ensures the
/// callback is not applied to said operations and its children.
template <typename... Ops>
void walkRegionSkipping(mlir::Region ®ion,
mlir::function_ref<void(mlir::Operation *)> callback) {
region.walk<mlir::WalkOrder::PreOrder>([&](mlir::Operation *op) {
if (isa<Ops...>(op))
return mlir::WalkResult::skip();
callback(op);
return mlir::WalkResult::advance();
});
}
/// Convert from a CIR comparison kind to an LLVM IR integral comparison kind.
mlir::LLVM::ICmpPredicate convertCmpKindToICmpPredicate(cir::CmpOpKind kind,
bool isSigned) {
using CIR = cir::CmpOpKind;
using LLVMICmp = mlir::LLVM::ICmpPredicate;
switch (kind) {
case CIR::eq:
return LLVMICmp::eq;
case CIR::ne:
return LLVMICmp::ne;
case CIR::lt:
return (isSigned ? LLVMICmp::slt : LLVMICmp::ult);
case CIR::le:
return (isSigned ? LLVMICmp::sle : LLVMICmp::ule);
case CIR::gt:
return (isSigned ? LLVMICmp::sgt : LLVMICmp::ugt);
case CIR::ge:
return (isSigned ? LLVMICmp::sge : LLVMICmp::uge);
}
llvm_unreachable("Unknown CmpOpKind");
}
/// Convert from a CIR comparison kind to an LLVM IR floating-point comparison
/// kind.
mlir::LLVM::FCmpPredicate convertCmpKindToFCmpPredicate(cir::CmpOpKind kind) {
using CIR = cir::CmpOpKind;
using LLVMFCmp = mlir::LLVM::FCmpPredicate;
switch (kind) {
case CIR::eq:
return LLVMFCmp::oeq;
case CIR::ne:
return LLVMFCmp::une;
case CIR::lt:
return LLVMFCmp::olt;
case CIR::le:
return LLVMFCmp::ole;
case CIR::gt:
return LLVMFCmp::ogt;
case CIR::ge:
return LLVMFCmp::oge;
}
llvm_unreachable("Unknown CmpOpKind");
}
/// If the given type is a vector type, return the vector's element type.
/// Otherwise return the given type unchanged.
mlir::Type elementTypeIfVector(mlir::Type type) {
if (auto VecType = mlir::dyn_cast<cir::VectorType>(type)) {
return VecType.getEltType();
}
return type;
}
mlir::LLVM::Visibility
lowerCIRVisibilityToLLVMVisibility(cir::VisibilityKind visibilityKind) {
switch (visibilityKind) {
case cir::VisibilityKind::Default:
return ::mlir::LLVM::Visibility::Default;
case cir::VisibilityKind::Hidden:
return ::mlir::LLVM::Visibility::Hidden;
case cir::VisibilityKind::Protected:
return ::mlir::LLVM::Visibility::Protected;
}
}
// Make sure the LLVM function we are about to create a call for actually
// exists, if not create one. Returns a function
void getOrCreateLLVMFuncOp(mlir::ConversionPatternRewriter &rewriter,
mlir::Operation *srcOp, llvm::StringRef fnName,
mlir::Type fnTy) {
auto modOp = srcOp->getParentOfType<mlir::ModuleOp>();
auto enclosingFnOp = srcOp->getParentOfType<mlir::LLVM::LLVMFuncOp>();
auto *sourceSymbol = mlir::SymbolTable::lookupSymbolIn(modOp, fnName);
if (!sourceSymbol) {
mlir::OpBuilder::InsertionGuard guard(rewriter);
rewriter.setInsertionPoint(enclosingFnOp);
rewriter.create<mlir::LLVM::LLVMFuncOp>(srcOp->getLoc(), fnName, fnTy);
}
}
static constexpr StringRef llvmMetadataSectionName = "llvm.metadata";
// Create a string global for annotation related string.
mlir::LLVM::GlobalOp
getAnnotationStringGlobal(mlir::StringAttr strAttr, mlir::ModuleOp &module,
llvm::StringMap<mlir::LLVM::GlobalOp> &globalsMap,
mlir::OpBuilder &globalVarBuilder,
mlir::Location &loc, bool isArg = false) {
llvm::StringRef str = strAttr.getValue();
if (!globalsMap.contains(str)) {
auto llvmStrTy = mlir::LLVM::LLVMArrayType::get(
mlir::IntegerType::get(module.getContext(), 8), str.size() + 1);
auto strGlobalOp = globalVarBuilder.create<mlir::LLVM::GlobalOp>(
loc, llvmStrTy,
/*isConstant=*/true, mlir::LLVM::Linkage::Private,
".str" +
(globalsMap.empty() ? ""
: "." + std::to_string(globalsMap.size())) +
".annotation" + (isArg ? ".arg" : ""),
mlir::StringAttr::get(module.getContext(), std::string(str) + '\0'),
/*alignment=*/isArg ? 1 : 0);
if (!isArg)
strGlobalOp.setSection(llvmMetadataSectionName);
strGlobalOp.setUnnamedAddr(mlir::LLVM::UnnamedAddr::Global);
strGlobalOp.setDsoLocal(true);
globalsMap[str] = strGlobalOp;
}
return globalsMap[str];
}
mlir::LLVM::GlobalOp getOrCreateAnnotationArgsVar(
mlir::Location &loc, mlir::ModuleOp &module,
mlir::OpBuilder &globalVarBuilder,
llvm::StringMap<mlir::LLVM::GlobalOp> &argStringGlobalsMap,
llvm::MapVector<mlir::ArrayAttr, mlir::LLVM::GlobalOp> &argsVarMap,
mlir::ArrayAttr argsAttr) {
if (argsVarMap.contains(argsAttr))
return argsVarMap[argsAttr];
mlir::LLVM::LLVMPointerType annoPtrTy =
mlir::LLVM::LLVMPointerType::get(globalVarBuilder.getContext());
llvm::SmallVector<mlir::Type> argStrutFldTypes;
llvm::SmallVector<mlir::Value> argStrutFields;
for (mlir::Attribute arg : argsAttr) {
if (auto strArgAttr = mlir::dyn_cast<mlir::StringAttr>(arg)) {
// Call getAnnotationStringGlobal here to make sure
// have a global for this string before
// creation of the args var.
getAnnotationStringGlobal(strArgAttr, module, argStringGlobalsMap,
globalVarBuilder, loc, true);
// This will become a ptr to the global string
argStrutFldTypes.push_back(annoPtrTy);
} else if (auto intArgAttr = mlir::dyn_cast<mlir::IntegerAttr>(arg)) {
argStrutFldTypes.push_back(intArgAttr.getType());
} else {
llvm_unreachable("Unsupported annotation arg type");
}
}
mlir::LLVM::LLVMStructType argsStructTy =
mlir::LLVM::LLVMStructType::getLiteral(globalVarBuilder.getContext(),
argStrutFldTypes);
auto argsGlobalOp = globalVarBuilder.create<mlir::LLVM::GlobalOp>(
loc, argsStructTy, true, mlir::LLVM::Linkage::Private,
".args" +
(argsVarMap.empty() ? "" : "." + std::to_string(argsVarMap.size())) +
".annotation",
mlir::Attribute());
argsGlobalOp.setSection(llvmMetadataSectionName);
argsGlobalOp.setUnnamedAddr(mlir::LLVM::UnnamedAddr::Global);
argsGlobalOp.setDsoLocal(true);
// Create the initializer for this args global
argsGlobalOp.getRegion().push_back(new mlir::Block());
mlir::OpBuilder argsInitBuilder(module.getContext());
argsInitBuilder.setInsertionPointToEnd(argsGlobalOp.getInitializerBlock());
mlir::Value argsStructInit =
argsInitBuilder.create<mlir::LLVM::UndefOp>(loc, argsStructTy);
int idx = 0;
for (mlir::Attribute arg : argsAttr) {
if (auto strArgAttr = mlir::dyn_cast<mlir::StringAttr>(arg)) {
// This would be simply return with existing map entry value
// from argStringGlobalsMap as string global is already
// created in the previous loop.
mlir::LLVM::GlobalOp argStrVar = getAnnotationStringGlobal(
strArgAttr, module, argStringGlobalsMap, globalVarBuilder, loc, true);
auto argStrVarAddr = argsInitBuilder.create<mlir::LLVM::AddressOfOp>(
loc, annoPtrTy, argStrVar.getSymName());
argsStructInit = argsInitBuilder.create<mlir::LLVM::InsertValueOp>(
loc, argsStructInit, argStrVarAddr, idx++);
} else if (auto intArgAttr = mlir::dyn_cast<mlir::IntegerAttr>(arg)) {
auto intArgFld = argsInitBuilder.create<mlir::LLVM::ConstantOp>(
loc, intArgAttr.getType(), intArgAttr.getValue());
argsStructInit = argsInitBuilder.create<mlir::LLVM::InsertValueOp>(
loc, argsStructInit, intArgFld, idx++);
} else {
llvm_unreachable("Unsupported annotation arg type");
}
}
argsInitBuilder.create<mlir::LLVM::ReturnOp>(loc, argsStructInit);
argsVarMap[argsAttr] = argsGlobalOp;
return argsGlobalOp;
}
/// Lower an annotation value to a series of LLVM globals, `outVals` contains
/// all values which are either used to build other globals or for intrisic call
/// arguments.
void lowerAnnotationValue(
mlir::Location &localLoc, mlir::Location annotLoc,
cir::AnnotationAttr annotation, mlir::ModuleOp &module,
mlir::OpBuilder &varInitBuilder, mlir::OpBuilder &globalVarBuilder,
llvm::StringMap<mlir::LLVM::GlobalOp> &stringGlobalsMap,
llvm::StringMap<mlir::LLVM::GlobalOp> &argStringGlobalsMap,
llvm::MapVector<mlir::ArrayAttr, mlir::LLVM::GlobalOp> &argsVarMap,
SmallVectorImpl<mlir::Value> &outVals) {
mlir::LLVM::LLVMPointerType annoPtrTy =
mlir::LLVM::LLVMPointerType::get(globalVarBuilder.getContext());
// First field is either a global name or a alloca address and is handled
// by the caller, this function deals with content from `AnnotationAttr`
// only.
// The second field is ptr to the annotation name
mlir::StringAttr annotationName = annotation.getName();
auto annotationNameFld = varInitBuilder.create<mlir::LLVM::AddressOfOp>(
localLoc, annoPtrTy,
getAnnotationStringGlobal(annotationName, module, stringGlobalsMap,
globalVarBuilder, localLoc)
.getSymName());
outVals.push_back(annotationNameFld->getResult(0));
// The third field is ptr to the translation unit name,
// and the fourth field is the line number
if (mlir::isa<mlir::FusedLoc>(annotLoc)) {
auto FusedLoc = mlir::cast<mlir::FusedLoc>(annotLoc);
annotLoc = FusedLoc.getLocations()[0];
}
auto annotFileLoc = mlir::cast<mlir::FileLineColLoc>(annotLoc);
assert(annotFileLoc && "annotation value has to be FileLineColLoc");
// To be consistent with clang code gen, we add trailing null char
auto fileName = mlir::StringAttr::get(
module.getContext(), std::string(annotFileLoc.getFilename().getValue()));
auto fileNameFld = varInitBuilder.create<mlir::LLVM::AddressOfOp>(
localLoc, annoPtrTy,
getAnnotationStringGlobal(fileName, module, stringGlobalsMap,
globalVarBuilder, localLoc)
.getSymName());
outVals.push_back(fileNameFld->getResult(0));
unsigned int lineNo = annotFileLoc.getLine();
auto lineNoFld = varInitBuilder.create<mlir::LLVM::ConstantOp>(
localLoc, globalVarBuilder.getI32Type(), lineNo);
outVals.push_back(lineNoFld->getResult(0));
// The fifth field is ptr to the annotation args var, it could be null
if (annotation.isNoArgs()) {
auto nullPtrFld =
varInitBuilder.create<mlir::LLVM::ZeroOp>(localLoc, annoPtrTy);
outVals.push_back(nullPtrFld->getResult(0));
} else {
mlir::ArrayAttr argsAttr = annotation.getArgs();
mlir::LLVM::GlobalOp annotArgsVar =
getOrCreateAnnotationArgsVar(localLoc, module, globalVarBuilder,
argStringGlobalsMap, argsVarMap, argsAttr);
auto argsVarView = varInitBuilder.create<mlir::LLVM::AddressOfOp>(
localLoc, annoPtrTy, annotArgsVar.getSymName());
outVals.push_back(argsVarView->getResult(0));
}
}
// Get addrspace by converting a pointer type.
// TODO: The approach here is a little hacky. We should access the target info
// directly to convert the address space of global op, similar to what we do
// for type converter.
unsigned getGlobalOpTargetAddrSpace(mlir::ConversionPatternRewriter &rewriter,
const mlir::TypeConverter *converter,
cir::GlobalOp op) {
auto tempPtrTy = cir::PointerType::get(rewriter.getContext(), op.getSymType(),
op.getAddrSpaceAttr());
return cast<mlir::LLVM::LLVMPointerType>(converter->convertType(tempPtrTy))
.getAddressSpace();
}
/// Given a type convertor and a data layout, convert the given type to a type
/// that is suitable for memory operations. For example, this can be used to
/// lower cir.bool accesses to i8.
static mlir::Type convertTypeForMemory(const mlir::TypeConverter &converter,
mlir::DataLayout const &dataLayout,
mlir::Type type) {
// TODO(cir): Handle other types similarly to clang's codegen
// convertTypeForMemory
if (isa<cir::BoolType>(type)) {
return mlir::IntegerType::get(type.getContext(),
dataLayout.getTypeSizeInBits(type));
}
return converter.convertType(type);
}
/// Emits the value from memory as expected by its users. Should be called when
/// the memory represetnation of a CIR type is not equal to its scalar
/// representation.
static mlir::Value emitFromMemory(mlir::ConversionPatternRewriter &rewriter,
mlir::DataLayout const &dataLayout,
cir::LoadOp op, mlir::Value value) {
// TODO(cir): Handle other types similarly to clang's codegen EmitFromMemory
if (auto boolTy = mlir::dyn_cast<cir::BoolType>(op.getResult().getType())) {
// Create a cast value from specified size in datalayout to i1
assert(value.getType().isInteger(dataLayout.getTypeSizeInBits(boolTy)));
return createIntCast(rewriter, value, rewriter.getI1Type());
}
return value;
}
/// Emits a value to memory with the expected scalar type. Should be called when
/// the memory represetnation of a CIR type is not equal to its scalar
/// representation.
static mlir::Value emitToMemory(mlir::ConversionPatternRewriter &rewriter,
mlir::DataLayout const &dataLayout,
mlir::Type origType, mlir::Value value) {
// TODO(cir): Handle other types similarly to clang's codegen EmitToMemory
if (auto boolTy = mlir::dyn_cast<cir::BoolType>(origType)) {
// Create zext of value from i1 to i8
auto memType =
rewriter.getIntegerType(dataLayout.getTypeSizeInBits(boolTy));
return createIntCast(rewriter, value, memType);
}
return value;
}
} // namespace
//===----------------------------------------------------------------------===//
// Visitors for Lowering CIR Const Attributes
//===----------------------------------------------------------------------===//
/// Emits a value to memory with the expected scalar type. Should be called when
/// the memory represetnation of a CIR attribute's type is not equal to its
/// scalar representation.
static mlir::Value
emitCirAttrToMemory(mlir::Operation *parentOp, mlir::Attribute attr,
mlir::ConversionPatternRewriter &rewriter,
const mlir::TypeConverter *converter,
mlir::DataLayout const &dataLayout) {
mlir::Value loweredValue =
lowerCirAttrAsValue(parentOp, attr, rewriter, converter, dataLayout);
if (auto boolAttr = mlir::dyn_cast<cir::BoolAttr>(attr)) {
return emitToMemory(rewriter, dataLayout, boolAttr.getType(), loweredValue);
}
return loweredValue;
}
/// Switches on the type of attribute and calls the appropriate conversion.
class CirAttrToValue : public CirAttrVisitor<CirAttrToValue, mlir::Value> {
public:
CirAttrToValue(mlir::Operation *parentOp,
mlir::ConversionPatternRewriter &rewriter,
const mlir::TypeConverter *converter,
mlir::DataLayout const &dataLayout)
: parentOp(parentOp), rewriter(rewriter), converter(converter),
dataLayout(dataLayout) {}
mlir::Value visitCirIntAttr(cir::IntAttr attr);
mlir::Value visitCirFPAttr(cir::FPAttr attr);
mlir::Value visitCirConstPtrAttr(cir::ConstPtrAttr attr);
mlir::Value visitCirConstStructAttr(cir::ConstStructAttr attr);
mlir::Value visitCirConstArrayAttr(cir::ConstArrayAttr attr);
mlir::Value visitCirConstVectorAttr(cir::ConstVectorAttr attr);
mlir::Value visitCirBoolAttr(cir::BoolAttr attr);
mlir::Value visitCirZeroAttr(cir::ZeroAttr attr);
mlir::Value visitCirUndefAttr(cir::UndefAttr attr);
mlir::Value visitCirPoisonAttr(cir::PoisonAttr attr);
mlir::Value visitCirGlobalViewAttr(cir::GlobalViewAttr attr);
mlir::Value visitCirVTableAttr(cir::VTableAttr attr);
mlir::Value visitCirTypeInfoAttr(cir::TypeInfoAttr attr);
private:
mlir::Operation *parentOp;
mlir::ConversionPatternRewriter &rewriter;
const mlir::TypeConverter *converter;
mlir::DataLayout const &dataLayout;
};
/// IntAttr visitor.
mlir::Value CirAttrToValue::visitCirIntAttr(cir::IntAttr intAttr) {
auto loc = parentOp->getLoc();
return rewriter.create<mlir::LLVM::ConstantOp>(
loc, converter->convertType(intAttr.getType()), intAttr.getValue());
}
/// BoolAttr visitor.
mlir::Value CirAttrToValue::visitCirBoolAttr(cir::BoolAttr boolAttr) {
auto loc = parentOp->getLoc();
return rewriter.create<mlir::LLVM::ConstantOp>(
loc, converter->convertType(boolAttr.getType()), boolAttr.getValue());
}
/// ConstPtrAttr visitor.
mlir::Value CirAttrToValue::visitCirConstPtrAttr(cir::ConstPtrAttr ptrAttr) {
auto loc = parentOp->getLoc();
if (ptrAttr.isNullValue()) {
return rewriter.create<mlir::LLVM::ZeroOp>(
loc, converter->convertType(ptrAttr.getType()));
}
mlir::DataLayout layout(parentOp->getParentOfType<mlir::ModuleOp>());
mlir::Value ptrVal = rewriter.create<mlir::LLVM::ConstantOp>(
loc, rewriter.getIntegerType(layout.getTypeSizeInBits(ptrAttr.getType())),
ptrAttr.getValue().getInt());
return rewriter.create<mlir::LLVM::IntToPtrOp>(
loc, converter->convertType(ptrAttr.getType()), ptrVal);
}
/// FPAttr visitor.
mlir::Value CirAttrToValue::visitCirFPAttr(cir::FPAttr fltAttr) {
auto loc = parentOp->getLoc();
return rewriter.create<mlir::LLVM::ConstantOp>(
loc, converter->convertType(fltAttr.getType()), fltAttr.getValue());
}
/// ZeroAttr visitor.
mlir::Value CirAttrToValue::visitCirZeroAttr(cir::ZeroAttr zeroAttr) {
auto loc = parentOp->getLoc();
return rewriter.create<mlir::LLVM::ZeroOp>(
loc, converter->convertType(zeroAttr.getType()));
}
/// UndefAttr visitor.
mlir::Value CirAttrToValue::visitCirUndefAttr(cir::UndefAttr undefAttr) {
auto loc = parentOp->getLoc();
return rewriter.create<mlir::LLVM::UndefOp>(
loc, converter->convertType(undefAttr.getType()));
}
/// PoisonAttr visitor.
mlir::Value CirAttrToValue::visitCirPoisonAttr(cir::PoisonAttr poisonAttr) {
auto loc = parentOp->getLoc();
return rewriter.create<mlir::LLVM::PoisonOp>(
loc, converter->convertType(poisonAttr.getType()));
}
/// ConstStruct visitor.
mlir::Value
CirAttrToValue::visitCirConstStructAttr(cir::ConstStructAttr constStruct) {
auto llvmTy = converter->convertType(constStruct.getType());
auto loc = parentOp->getLoc();
mlir::Value result = rewriter.create<mlir::LLVM::UndefOp>(loc, llvmTy);
// Iteratively lower each constant element of the struct.
for (auto [idx, elt] : llvm::enumerate(constStruct.getMembers())) {
mlir::Value init =
emitCirAttrToMemory(parentOp, elt, rewriter, converter, dataLayout);
result = rewriter.create<mlir::LLVM::InsertValueOp>(loc, result, init, idx);
}
return result;
}
// VTableAttr visitor.
mlir::Value CirAttrToValue::visitCirVTableAttr(cir::VTableAttr vtableArr) {
auto llvmTy = converter->convertType(vtableArr.getType());
auto loc = parentOp->getLoc();
mlir::Value result = rewriter.create<mlir::LLVM::UndefOp>(loc, llvmTy);
for (auto [idx, elt] : llvm::enumerate(vtableArr.getVtableData())) {
mlir::Value init = visit(elt);
result = rewriter.create<mlir::LLVM::InsertValueOp>(loc, result, init, idx);
}
return result;
}
// TypeInfoAttr visitor.
mlir::Value
CirAttrToValue::visitCirTypeInfoAttr(cir::TypeInfoAttr typeinfoArr) {
auto llvmTy = converter->convertType(typeinfoArr.getType());
auto loc = parentOp->getLoc();
mlir::Value result = rewriter.create<mlir::LLVM::UndefOp>(loc, llvmTy);
for (auto [idx, elt] : llvm::enumerate(typeinfoArr.getData())) {
mlir::Value init = visit(elt);
result = rewriter.create<mlir::LLVM::InsertValueOp>(loc, result, init, idx);
}
return result;
}
// ConstArrayAttr visitor
mlir::Value
CirAttrToValue::visitCirConstArrayAttr(cir::ConstArrayAttr constArr) {
auto llvmTy = converter->convertType(constArr.getType());
auto loc = parentOp->getLoc();
mlir::Value result;
if (auto zeros = constArr.getTrailingZerosNum()) {
auto arrayTy = constArr.getType();
result = rewriter.create<mlir::LLVM::ZeroOp>(
loc, converter->convertType(arrayTy));
} else {
result = rewriter.create<mlir::LLVM::UndefOp>(loc, llvmTy);
}
// Iteratively lower each constant element of the array.
if (auto arrayAttr = mlir::dyn_cast<mlir::ArrayAttr>(constArr.getElts())) {
for (auto [idx, elt] : llvm::enumerate(arrayAttr)) {
mlir::Value init =
emitCirAttrToMemory(parentOp, elt, rewriter, converter, dataLayout);
result =
rewriter.create<mlir::LLVM::InsertValueOp>(loc, result, init, idx);
}
}
// TODO(cir): this diverges from traditional lowering. Normally the string
// would be a global constant that is memcopied.
else if (auto strAttr =
mlir::dyn_cast<mlir::StringAttr>(constArr.getElts())) {
auto arrayTy = mlir::dyn_cast<cir::ArrayType>(strAttr.getType());
assert(arrayTy && "String attribute must have an array type");
auto eltTy = arrayTy.getEltType();
for (auto [idx, elt] : llvm::enumerate(strAttr)) {
auto init = rewriter.create<mlir::LLVM::ConstantOp>(
loc, converter->convertType(eltTy), elt);
result =
rewriter.create<mlir::LLVM::InsertValueOp>(loc, result, init, idx);
}
} else {
llvm_unreachable("unexpected ConstArrayAttr elements");
}
return result;
}
// ConstVectorAttr visitor.
mlir::Value
CirAttrToValue::visitCirConstVectorAttr(cir::ConstVectorAttr constVec) {
auto llvmTy = converter->convertType(constVec.getType());
auto loc = parentOp->getLoc();
SmallVector<mlir::Attribute> mlirValues;
for (auto elementAttr : constVec.getElts()) {
mlir::Attribute mlirAttr;
if (auto intAttr = mlir::dyn_cast<cir::IntAttr>(elementAttr)) {
mlirAttr = rewriter.getIntegerAttr(
converter->convertType(intAttr.getType()), intAttr.getValue());
} else if (auto floatAttr = mlir::dyn_cast<cir::FPAttr>(elementAttr)) {
mlirAttr = rewriter.getFloatAttr(
converter->convertType(floatAttr.getType()), floatAttr.getValue());
} else {
llvm_unreachable(
"vector constant with an element that is neither an int nor a float");
}
mlirValues.push_back(mlirAttr);
}
return rewriter.create<mlir::LLVM::ConstantOp>(
loc, llvmTy,
mlir::DenseElementsAttr::get(mlir::cast<mlir::ShapedType>(llvmTy),
mlirValues));
}
// GlobalViewAttr visitor.
mlir::Value
CirAttrToValue::visitCirGlobalViewAttr(cir::GlobalViewAttr globalAttr) {
auto module = parentOp->getParentOfType<mlir::ModuleOp>();
mlir::Type sourceType;
unsigned sourceAddrSpace = 0;
llvm::StringRef symName;
auto *sourceSymbol =
mlir::SymbolTable::lookupSymbolIn(module, globalAttr.getSymbol());
if (auto llvmSymbol = dyn_cast<mlir::LLVM::GlobalOp>(sourceSymbol)) {
sourceType = llvmSymbol.getType();
symName = llvmSymbol.getSymName();
sourceAddrSpace = llvmSymbol.getAddrSpace();
} else if (auto cirSymbol = dyn_cast<cir::GlobalOp>(sourceSymbol)) {
sourceType =
convertTypeForMemory(*converter, dataLayout, cirSymbol.getSymType());
symName = cirSymbol.getSymName();
sourceAddrSpace =
getGlobalOpTargetAddrSpace(rewriter, converter, cirSymbol);
} else if (auto llvmFun = dyn_cast<mlir::LLVM::LLVMFuncOp>(sourceSymbol)) {
sourceType = llvmFun.getFunctionType();
symName = llvmFun.getSymName();
sourceAddrSpace = 0;
} else if (auto fun = dyn_cast<cir::FuncOp>(sourceSymbol)) {
sourceType = converter->convertType(fun.getFunctionType());
symName = fun.getSymName();
sourceAddrSpace = 0;
} else {
llvm_unreachable("Unexpected GlobalOp type");
}
auto loc = parentOp->getLoc();
mlir::Value addrOp = rewriter.create<mlir::LLVM::AddressOfOp>(
loc,
mlir::LLVM::LLVMPointerType::get(rewriter.getContext(), sourceAddrSpace),
symName);
if (globalAttr.getIndices()) {
llvm::SmallVector<mlir::LLVM::GEPArg> indices;
if (isa<mlir::LLVM::LLVMArrayType, mlir::LLVM::LLVMStructType>(sourceType))
indices.push_back(0);
for (auto idx : globalAttr.getIndices()) {
auto intAttr = dyn_cast<mlir::IntegerAttr>(idx);
assert(intAttr && "index must be integers");
indices.push_back(intAttr.getValue().getSExtValue());
}
auto resTy = addrOp.getType();
auto eltTy = converter->convertType(sourceType);
addrOp = rewriter.create<mlir::LLVM::GEPOp>(loc, resTy, eltTy, addrOp,
indices, true);
}
if (auto intTy = mlir::dyn_cast<cir::IntType>(globalAttr.getType())) {
auto llvmDstTy = converter->convertType(globalAttr.getType());
return rewriter.create<mlir::LLVM::PtrToIntOp>(parentOp->getLoc(),
llvmDstTy, addrOp);
}
if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(globalAttr.getType())) {
auto llvmEltTy =
convertTypeForMemory(*converter, dataLayout, ptrTy.getPointee());
if (llvmEltTy == sourceType)
return addrOp;
auto llvmDstTy = converter->convertType(globalAttr.getType());
return rewriter.create<mlir::LLVM::BitcastOp>(parentOp->getLoc(), llvmDstTy,
addrOp);
}
llvm_unreachable("Expecting pointer or integer type for GlobalViewAttr");
}
/// Switches on the type of attribute and calls the appropriate conversion.
mlir::Value lowerCirAttrAsValue(mlir::Operation *parentOp,
const mlir::Attribute attr,
mlir::ConversionPatternRewriter &rewriter,
const mlir::TypeConverter *converter,
mlir::DataLayout const &dataLayout) {
CirAttrToValue valueConverter(parentOp, rewriter, converter, dataLayout);
auto value = valueConverter.visit(attr);
if (!value)
llvm_unreachable("unhandled attribute type");
return value;
}
//===----------------------------------------------------------------------===//
mlir::LLVM::Linkage convertLinkage(cir::GlobalLinkageKind linkage) {
using CIR = cir::GlobalLinkageKind;
using LLVM = mlir::LLVM::Linkage;
switch (linkage) {
case CIR::AvailableExternallyLinkage:
return LLVM::AvailableExternally;
case CIR::CommonLinkage:
return LLVM::Common;
case CIR::ExternalLinkage:
return LLVM::External;
case CIR::ExternalWeakLinkage:
return LLVM::ExternWeak;
case CIR::InternalLinkage:
return LLVM::Internal;
case CIR::LinkOnceAnyLinkage:
return LLVM::Linkonce;
case CIR::LinkOnceODRLinkage:
return LLVM::LinkonceODR;
case CIR::PrivateLinkage:
return LLVM::Private;
case CIR::WeakAnyLinkage:
return LLVM::Weak;
case CIR::WeakODRLinkage:
return LLVM::WeakODR;
};
}
mlir::LLVM::CConv convertCallingConv(cir::CallingConv callinvConv) {
using CIR = cir::CallingConv;
using LLVM = mlir::LLVM::CConv;
switch (callinvConv) {
case CIR::C:
return LLVM::C;
case CIR::SpirKernel:
return LLVM::SPIR_KERNEL;
case CIR::SpirFunction:
return LLVM::SPIR_FUNC;
}
llvm_unreachable("Unknown calling convention");
}
void convertSideEffectForCall(mlir::Operation *callOp,
cir::SideEffect sideEffect,
mlir::LLVM::MemoryEffectsAttr &memoryEffect,
bool &noUnwind, bool &willReturn) {
using mlir::LLVM::ModRefInfo;
switch (sideEffect) {
case cir::SideEffect::All:
memoryEffect = {};
noUnwind = false;
willReturn = false;
break;
case cir::SideEffect::Pure:
memoryEffect = mlir::LLVM::MemoryEffectsAttr::get(
callOp->getContext(), /*other=*/ModRefInfo::Ref,
/*argMem=*/ModRefInfo::Ref,
/*inaccessibleMem=*/ModRefInfo::Ref);
noUnwind = true;
willReturn = true;
break;
case cir::SideEffect::Const:
memoryEffect = mlir::LLVM::MemoryEffectsAttr::get(
callOp->getContext(), /*other=*/ModRefInfo::NoModRef,
/*argMem=*/ModRefInfo::NoModRef,
/*inaccessibleMem=*/ModRefInfo::NoModRef);
noUnwind = true;
willReturn = true;
break;
}
}
mlir::LogicalResult CIRToLLVMCopyOpLowering::matchAndRewrite(
cir::CopyOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
const mlir::Value length = rewriter.create<mlir::LLVM::ConstantOp>(
op.getLoc(), rewriter.getI32Type(), op.getLength());
rewriter.replaceOpWithNewOp<mlir::LLVM::MemcpyOp>(
op, adaptor.getDst(), adaptor.getSrc(), length, op.getIsVolatile());
return mlir::success();
}
mlir::LogicalResult CIRToLLVMMemCpyOpLowering::matchAndRewrite(
cir::MemCpyOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
rewriter.replaceOpWithNewOp<mlir::LLVM::MemcpyOp>(
op, adaptor.getDst(), adaptor.getSrc(), adaptor.getLen(),
/*isVolatile=*/false);
return mlir::success();
}
mlir::LogicalResult CIRToLLVMMemChrOpLowering::matchAndRewrite(
cir::MemChrOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
auto llvmPtrTy = mlir::LLVM::LLVMPointerType::get(rewriter.getContext());
llvm::SmallVector<mlir::Type> arguments;
const mlir::TypeConverter *converter = getTypeConverter();
mlir::Type srcTy = converter->convertType(op.getSrc().getType());
mlir::Type patternTy = converter->convertType(op.getPattern().getType());
mlir::Type lenTy = converter->convertType(op.getLen().getType());
auto fnTy =
mlir::LLVM::LLVMFunctionType::get(llvmPtrTy, {srcTy, patternTy, lenTy},
/*isVarArg=*/false);
llvm::StringRef fnName = "memchr";
getOrCreateLLVMFuncOp(rewriter, op, fnName, fnTy);
rewriter.replaceOpWithNewOp<mlir::LLVM::CallOp>(
op, mlir::TypeRange{llvmPtrTy}, fnName,
mlir::ValueRange{adaptor.getSrc(), adaptor.getPattern(),
adaptor.getLen()});
return mlir::success();
}
mlir::LogicalResult CIRToLLVMMemMoveOpLowering::matchAndRewrite(
cir::MemMoveOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
rewriter.replaceOpWithNewOp<mlir::LLVM::MemmoveOp>(
op, adaptor.getDst(), adaptor.getSrc(), adaptor.getLen(),
/*isVolatile=*/false);
return mlir::success();
}
mlir::LogicalResult CIRToLLVMMemCpyInlineOpLowering::matchAndRewrite(
cir::MemCpyInlineOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
rewriter.replaceOpWithNewOp<mlir::LLVM::MemcpyInlineOp>(
op, adaptor.getDst(), adaptor.getSrc(), adaptor.getLenAttr(),
/*isVolatile=*/false);
return mlir::success();
}
mlir::LogicalResult CIRToLLVMMemSetOpLowering::matchAndRewrite(
cir::MemSetOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
auto converted = rewriter.create<mlir::LLVM::TruncOp>(
op.getLoc(), mlir::IntegerType::get(op.getContext(), 8),
adaptor.getVal());
rewriter.replaceOpWithNewOp<mlir::LLVM::MemsetOp>(op, adaptor.getDst(),
converted, adaptor.getLen(),
/*isVolatile=*/false);
return mlir::success();
}
mlir::LogicalResult CIRToLLVMMemSetInlineOpLowering::matchAndRewrite(
cir::MemSetInlineOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
auto converted = rewriter.create<mlir::LLVM::TruncOp>(
op.getLoc(), mlir::IntegerType::get(op.getContext(), 8),
adaptor.getVal());
rewriter.replaceOpWithNewOp<mlir::LLVM::MemsetInlineOp>(
op, adaptor.getDst(), converted, adaptor.getLenAttr(),
/*isVolatile=*/false);
return mlir::success();
}
static mlir::Value getLLVMIntCast(mlir::ConversionPatternRewriter &rewriter,
mlir::Value llvmSrc, mlir::Type llvmDstIntTy,
bool isUnsigned, uint64_t cirSrcWidth,
uint64_t cirDstIntWidth) {
if (cirSrcWidth == cirDstIntWidth)
return llvmSrc;
auto loc = llvmSrc.getLoc();
if (cirSrcWidth < cirDstIntWidth) {
if (isUnsigned)
return rewriter.create<mlir::LLVM::ZExtOp>(loc, llvmDstIntTy, llvmSrc);
return rewriter.create<mlir::LLVM::SExtOp>(loc, llvmDstIntTy, llvmSrc);
}
// Otherwise truncate
return rewriter.create<mlir::LLVM::TruncOp>(loc, llvmDstIntTy, llvmSrc);
}
mlir::LogicalResult CIRToLLVMPtrStrideOpLowering::matchAndRewrite(
cir::PtrStrideOp ptrStrideOp, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
auto *tc = getTypeConverter();
const auto resultTy = tc->convertType(ptrStrideOp.getType());
auto elementTy =
convertTypeForMemory(*tc, dataLayout, ptrStrideOp.getElementTy());
auto *ctx = elementTy.getContext();
// void and function types doesn't really have a layout to use in GEPs,
// make it i8 instead.
if (mlir::isa<mlir::LLVM::LLVMVoidType>(elementTy) ||
mlir::isa<mlir::LLVM::LLVMFunctionType>(elementTy))
elementTy = mlir::IntegerType::get(elementTy.getContext(), 8,
mlir::IntegerType::Signless);
// Zero-extend, sign-extend or trunc the pointer value.
auto index = adaptor.getStride();
auto width = mlir::cast<mlir::IntegerType>(index.getType()).getWidth();
mlir::DataLayout LLVMLayout(ptrStrideOp->getParentOfType<mlir::ModuleOp>());
auto layoutWidth =
LLVMLayout.getTypeIndexBitwidth(adaptor.getBase().getType());
auto indexOp = index.getDefiningOp();
if (indexOp && layoutWidth && width != *layoutWidth) {
// If the index comes from a subtraction, make sure the extension happens
// before it. To achieve that, look at unary minus, which already got
// lowered to "sub 0, x".
auto sub = dyn_cast<mlir::LLVM::SubOp>(indexOp);
auto unary = dyn_cast_if_present<cir::UnaryOp>(
ptrStrideOp.getStride().getDefiningOp());
bool rewriteSub =
unary && unary.getKind() == cir::UnaryOpKind::Minus && sub;
if (rewriteSub)
index = indexOp->getOperand(1);
// Handle the cast
auto llvmDstType = mlir::IntegerType::get(ctx, *layoutWidth);
index = getLLVMIntCast(rewriter, index, llvmDstType,
ptrStrideOp.getStride().getType().isUnsigned(),
width, *layoutWidth);
// Rewrite the sub in front of extensions/trunc
if (rewriteSub) {
index = rewriter.create<mlir::LLVM::SubOp>(
index.getLoc(), index.getType(),
rewriter.create<mlir::LLVM::ConstantOp>(
index.getLoc(), index.getType(),
mlir::IntegerAttr::get(index.getType(), 0)),
index);
rewriter.eraseOp(sub);
}
}
rewriter.replaceOpWithNewOp<mlir::LLVM::GEPOp>(
ptrStrideOp, resultTy, elementTy, adaptor.getBase(), index);
return mlir::success();
}
mlir::LogicalResult CIRToLLVMBaseClassAddrOpLowering::matchAndRewrite(
cir::BaseClassAddrOp baseClassOp, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
const auto resultType =
getTypeConverter()->convertType(baseClassOp.getType());
mlir::Value derivedAddr = adaptor.getDerivedAddr();
llvm::SmallVector<mlir::LLVM::GEPArg, 1> offset = {
adaptor.getOffset().getZExtValue()};
mlir::Type byteType = mlir::IntegerType::get(resultType.getContext(), 8,
mlir::IntegerType::Signless);
if (adaptor.getOffset().getZExtValue() == 0) {
rewriter.replaceOpWithNewOp<mlir::LLVM::BitcastOp>(
baseClassOp, resultType, adaptor.getDerivedAddr());
return mlir::success();
}
if (baseClassOp.getAssumeNotNull()) {
rewriter.replaceOpWithNewOp<mlir::LLVM::GEPOp>(
baseClassOp, resultType, byteType, derivedAddr, offset);
} else {
auto loc = baseClassOp.getLoc();
mlir::Value isNull = rewriter.create<mlir::LLVM::ICmpOp>(
loc, mlir::LLVM::ICmpPredicate::eq, derivedAddr,
rewriter.create<mlir::LLVM::ZeroOp>(loc, derivedAddr.getType()));
mlir::Value adjusted = rewriter.create<mlir::LLVM::GEPOp>(
loc, resultType, byteType, derivedAddr, offset);
rewriter.replaceOpWithNewOp<mlir::LLVM::SelectOp>(baseClassOp, isNull,
derivedAddr, adjusted);
}
return mlir::success();
}
mlir::LogicalResult CIRToLLVMDerivedClassAddrOpLowering::matchAndRewrite(
cir::DerivedClassAddrOp derivedClassOp, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
const auto resultType =
getTypeConverter()->convertType(derivedClassOp.getType());
mlir::Value baseAddr = adaptor.getBaseAddr();
int64_t offsetVal = adaptor.getOffset().getZExtValue() * -1;
llvm::SmallVector<mlir::LLVM::GEPArg, 1> offset = {offsetVal};
mlir::Type byteType = mlir::IntegerType::get(resultType.getContext(), 8,
mlir::IntegerType::Signless);
if (derivedClassOp.getAssumeNotNull()) {
rewriter.replaceOpWithNewOp<mlir::LLVM::GEPOp>(derivedClassOp, resultType,
byteType, baseAddr, offset);