Jlm
TypeConverterTests.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2025 Nico Reißmann <nico.reissmann@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #include <gtest/gtest.h>
7 
10 #include <jlm/rvsdg/control.hpp>
12 
13 #include <llvm/IR/DerivedTypes.h>
14 #include <llvm/IR/LLVMContext.h>
15 
16 TEST(TypeConverterTests, LlvmIntegerTypeConversion)
17 {
18  using namespace jlm::llvm;
19 
20  // Arrange
21  llvm::LLVMContext context;
22  TypeConverter typeConverter;
23 
24  const auto i1 = ::llvm::IntegerType::get(context, 1);
25  const auto i2 = ::llvm::IntegerType::get(context, 2);
26  const auto i4 = ::llvm::IntegerType::get(context, 4);
27  const auto i8 = ::llvm::IntegerType::get(context, 8);
28  const auto i16 = ::llvm::IntegerType::get(context, 16);
29  const auto i32 = ::llvm::IntegerType::get(context, 32);
30  const auto i64 = ::llvm::IntegerType::get(context, 64);
31 
32  // Act
33  const auto i1BitType =
34  std::dynamic_pointer_cast<const jlm::rvsdg::BitType>(typeConverter.ConvertLlvmType(*i1));
35  const auto i2BitType =
36  std::dynamic_pointer_cast<const jlm::rvsdg::BitType>(typeConverter.ConvertLlvmType(*i2));
37  const auto i4BitType =
38  std::dynamic_pointer_cast<const jlm::rvsdg::BitType>(typeConverter.ConvertLlvmType(*i4));
39  const auto i8BitType =
40  std::dynamic_pointer_cast<const jlm::rvsdg::BitType>(typeConverter.ConvertLlvmType(*i8));
41  const auto i16BitType =
42  std::dynamic_pointer_cast<const jlm::rvsdg::BitType>(typeConverter.ConvertLlvmType(*i16));
43  const auto i32BitType =
44  std::dynamic_pointer_cast<const jlm::rvsdg::BitType>(typeConverter.ConvertLlvmType(*i32));
45  const auto i64BitType =
46  std::dynamic_pointer_cast<const jlm::rvsdg::BitType>(typeConverter.ConvertLlvmType(*i64));
47 
48  // Assert
49  EXPECT_TRUE(i1BitType && i1BitType->nbits() == 1);
50  EXPECT_TRUE(i2BitType && i2BitType->nbits() == 2);
51  EXPECT_TRUE(i4BitType && i4BitType->nbits() == 4);
52  EXPECT_TRUE(i8BitType && i8BitType->nbits() == 8);
53  EXPECT_TRUE(i16BitType && i16BitType->nbits() == 16);
54  EXPECT_TRUE(i32BitType && i32BitType->nbits() == 32);
55  EXPECT_TRUE(i64BitType && i64BitType->nbits() == 64);
56 }
57 
58 TEST(TypeConverterTests, LlvmPointerTypeConversion)
59 {
60  using namespace jlm::llvm;
61 
62  // Arrange
63  llvm::LLVMContext context;
64  TypeConverter typeConverter;
65 
66  const auto pointerTypeLlvm = ::llvm::PointerType::get(context, 0);
67 
68  // Act
69  const auto pointerTypeJlm =
70  std::dynamic_pointer_cast<const PointerType>(typeConverter.ConvertLlvmType(*pointerTypeLlvm));
71 
72  // Assert
73  EXPECT_NE(pointerTypeJlm, nullptr);
74 }
75 
76 TEST(TypeConverterTests, LlvmFunctionTypeConversion)
77 {
78  using namespace jlm::llvm;
79  using namespace jlm::rvsdg;
80 
81  // Arrange
82  llvm::LLVMContext context;
83  TypeConverter typeConverter;
84 
85  const auto voidType = ::llvm::Type::getVoidTy(context);
86  auto i32Type = ::llvm::Type::getInt32Ty(context);
87  const auto functionType1Llvm = ::llvm::FunctionType::get(voidType, { i32Type, i32Type }, false);
88  const auto functionType2Llvm = ::llvm::FunctionType::get(i32Type, {}, false);
89  const auto functionType3Llvm = ::llvm::FunctionType::get(i32Type, { i32Type, i32Type }, true);
90 
91  // Act
92  const auto functionType1Jlm = std::dynamic_pointer_cast<const FunctionType>(
93  typeConverter.ConvertLlvmType(*functionType1Llvm));
94  const auto functionType2Jlm = std::dynamic_pointer_cast<const FunctionType>(
95  typeConverter.ConvertLlvmType(*functionType2Llvm));
96  const auto functionType3Jlm = std::dynamic_pointer_cast<const FunctionType>(
97  typeConverter.ConvertLlvmType(*functionType3Llvm));
98 
99  // Assert
100  EXPECT_NE(functionType1Jlm, nullptr);
101  EXPECT_EQ(functionType1Jlm->NumArguments(), 4u);
102  EXPECT_EQ(functionType1Jlm->NumResults(), 2u);
103  auto arguments = functionType1Jlm->Arguments();
104  EXPECT_TRUE(is<BitType>(arguments[0]));
105  EXPECT_TRUE(is<BitType>(arguments[1]));
106  EXPECT_TRUE(is<IOStateType>(arguments[2]));
107  EXPECT_TRUE(is<MemoryStateType>(arguments[3]));
108  auto results = functionType1Jlm->Results();
109  EXPECT_TRUE(is<IOStateType>(results[0]));
110  EXPECT_TRUE(is<MemoryStateType>(results[1]));
111 
112  EXPECT_NE(functionType2Jlm, nullptr);
113  EXPECT_EQ(functionType2Jlm->NumArguments(), 2u);
114  EXPECT_EQ(functionType2Jlm->NumResults(), 3u);
115  arguments = functionType2Jlm->Arguments();
116  EXPECT_TRUE(is<IOStateType>(arguments[0]));
117  EXPECT_TRUE(is<MemoryStateType>(arguments[1]));
118  results = functionType2Jlm->Results();
119  EXPECT_TRUE(is<BitType>(results[0]));
120  EXPECT_TRUE(is<IOStateType>(results[1]));
121  EXPECT_TRUE(is<MemoryStateType>(results[2]));
122 
123  EXPECT_NE(functionType3Jlm, nullptr);
124  EXPECT_EQ(functionType3Jlm->NumArguments(), 5u);
125  EXPECT_EQ(functionType3Jlm->NumResults(), 3u);
126  arguments = functionType3Jlm->Arguments();
127  EXPECT_TRUE(is<BitType>(arguments[0]));
128  EXPECT_TRUE(is<BitType>(arguments[1]));
129  EXPECT_TRUE(is<VariableArgumentType>(arguments[2]));
130  EXPECT_TRUE(is<IOStateType>(arguments[3]));
131  EXPECT_TRUE(is<MemoryStateType>(arguments[4]));
132  results = functionType3Jlm->Results();
133  EXPECT_TRUE(is<BitType>(results[0]));
134  EXPECT_TRUE(is<IOStateType>(results[1]));
135  EXPECT_TRUE(is<MemoryStateType>(results[2]));
136 }
137 
138 TEST(TypeConverterTests, LlvmFloatingPointTypeConversion)
139 {
140  using namespace jlm::llvm;
141 
142  // Arrange
143  llvm::LLVMContext context;
144  TypeConverter typeConverter;
145 
146  const auto halfTypeLlvm = ::llvm::Type::getHalfTy(context);
147  const auto floatTypeLlvm = ::llvm::Type::getFloatTy(context);
148  const auto doubleTypeLlvm = ::llvm::Type::getDoubleTy(context);
149  const auto x86fp80TypeLlvm = ::llvm::Type::getX86_FP80Ty(context);
150  const auto fp128TypeLlvm = ::llvm::Type::getFP128Ty(context);
151 
152  // Act
153  const auto halfTypeJlm = std::dynamic_pointer_cast<const FloatingPointType>(
154  typeConverter.ConvertLlvmType(*halfTypeLlvm));
155  const auto floatTypeJlm = std::dynamic_pointer_cast<const FloatingPointType>(
156  typeConverter.ConvertLlvmType(*floatTypeLlvm));
157  const auto doubleTypeJlm = std::dynamic_pointer_cast<const FloatingPointType>(
158  typeConverter.ConvertLlvmType(*doubleTypeLlvm));
159  const auto x86fp80TypeJlm = std::dynamic_pointer_cast<const FloatingPointType>(
160  typeConverter.ConvertLlvmType(*x86fp80TypeLlvm));
161  const auto fp128TypeJlm = std::dynamic_pointer_cast<const FloatingPointType>(
162  typeConverter.ConvertLlvmType(*fp128TypeLlvm));
163 
164  // Assert
165  EXPECT_TRUE(halfTypeJlm && halfTypeJlm->size() == fpsize::half);
166  EXPECT_TRUE(floatTypeJlm && floatTypeJlm->size() == fpsize::flt);
167  EXPECT_TRUE(doubleTypeJlm && doubleTypeJlm->size() == fpsize::dbl);
168  EXPECT_TRUE(x86fp80TypeJlm && x86fp80TypeJlm->size() == fpsize::x86fp80);
169  EXPECT_TRUE(fp128TypeJlm->size() == fpsize::fp128);
170 }
171 
172 TEST(TypeConverterTests, LlvmStructTypeConversion)
173 {
174  using namespace jlm::llvm;
175 
176  // Arrange
177  llvm::LLVMContext context;
178  TypeConverter typeConverter;
179 
180  auto i32Type = ::llvm::Type::getInt32Ty(context);
181  const auto halfType = ::llvm::Type::getHalfTy(context);
182  const auto structType1Llvm = ::llvm::StructType::get(context, { i32Type, halfType }, false);
183  // get() creates a literal struct without a name
184  const auto structType2Llvm =
185  ::llvm::StructType::get(context, { i32Type, i32Type, i32Type }, true);
186  const auto structType3Llvm = ::llvm::StructType::create(context, { i32Type }, "myStruct", true);
187  const auto structType4Llvm = ::llvm::StructType::create(context, { i32Type });
188 
189  // Act
190  const auto structType1Jlm =
191  std::dynamic_pointer_cast<const StructType>(typeConverter.ConvertLlvmType(*structType1Llvm));
192  const auto structType2Jlm =
193  std::dynamic_pointer_cast<const StructType>(typeConverter.ConvertLlvmType(*structType2Llvm));
194  const auto structType3Jlm =
195  std::dynamic_pointer_cast<const StructType>(typeConverter.ConvertLlvmType(*structType3Llvm));
196  const auto structType4Jlm =
197  std::dynamic_pointer_cast<const StructType>(typeConverter.ConvertLlvmType(*structType4Llvm));
198 
199  const auto structType5Jlm =
200  std::dynamic_pointer_cast<const StructType>(typeConverter.ConvertLlvmType(*structType1Llvm));
201 
202  // Assert
203  EXPECT_NE(structType1Jlm, nullptr);
204  EXPECT_EQ(structType1Jlm->numElements(), 2u);
205  EXPECT_FALSE(structType1Jlm->IsPacked());
206  EXPECT_TRUE(structType1Jlm->IsLiteral());
207  EXPECT_FALSE(structType1Jlm->HasName());
208 
209  EXPECT_NE(structType2Jlm, nullptr);
210  EXPECT_EQ(structType2Jlm->numElements(), 3u);
211  EXPECT_TRUE(structType2Jlm->IsPacked());
212  EXPECT_TRUE(structType2Jlm->IsLiteral());
213  EXPECT_FALSE(structType2Jlm->HasName());
214 
215  EXPECT_NE(structType3Jlm, nullptr);
216  EXPECT_EQ(structType3Jlm->numElements(), 1u);
217  EXPECT_TRUE(structType3Jlm->IsPacked());
218  EXPECT_FALSE(structType3Jlm->IsLiteral());
219  EXPECT_TRUE(structType3Jlm->HasName() && structType3Jlm->GetName() == "myStruct");
220 
221  EXPECT_NE(structType4Jlm, nullptr);
222  EXPECT_EQ(structType4Jlm->numElements(), 1u);
223  EXPECT_FALSE(structType4Jlm->IsPacked());
224  EXPECT_FALSE(structType4Jlm->IsLiteral());
225  EXPECT_FALSE(structType4Jlm->HasName());
226 
227  EXPECT_NE(structType1Jlm.get(), structType2Jlm.get());
228  EXPECT_NE(structType1Jlm.get(), structType3Jlm.get());
229  EXPECT_EQ(structType1Jlm.get(), structType5Jlm.get());
230  EXPECT_NE(structType2Jlm.get(), structType3Jlm.get());
231 }
232 
233 TEST(TypeConverterTests, LlvmArrayTypeConversion)
234 {
235  using namespace jlm::llvm;
236  using namespace jlm::rvsdg;
237 
238  // Arrange
239  llvm::LLVMContext context;
240  TypeConverter typeConverter;
241 
242  const auto i32Type = ::llvm::Type::getInt32Ty(context);
243  const auto halfType = ::llvm::Type::getHalfTy(context);
244  const auto arrayType1Llvm = ::llvm::ArrayType::get(i32Type, 4);
245  const auto arrayType2Llvm = ::llvm::ArrayType::get(halfType, 9);
246 
247  // Act
248  const auto arrayType1Jlm =
249  std::dynamic_pointer_cast<const ArrayType>(typeConverter.ConvertLlvmType(*arrayType1Llvm));
250  const auto arrayType2Jlm =
251  std::dynamic_pointer_cast<const ArrayType>(typeConverter.ConvertLlvmType(*arrayType2Llvm));
252 
253  // Assert
254  EXPECT_NE(arrayType1Jlm, nullptr);
255  EXPECT_TRUE(is<BitType>(arrayType1Jlm->element_type()));
256  EXPECT_EQ(arrayType1Jlm->nelements(), 4u);
257 
258  EXPECT_NE(arrayType2Jlm, nullptr);
259  EXPECT_TRUE(is<FloatingPointType>(arrayType2Jlm->element_type()));
260  EXPECT_EQ(arrayType2Jlm->nelements(), 9u);
261 }
262 
263 TEST(TypeConverterTests, LlvmVectorTypeConversion)
264 {
265  using namespace jlm::llvm;
266  using namespace jlm::rvsdg;
267 
268  // Arrange
269  llvm::LLVMContext context;
270  TypeConverter typeConverter;
271 
272  const auto i32Type = ::llvm::Type::getInt32Ty(context);
273  const auto halfType = ::llvm::Type::getHalfTy(context);
274  const auto vectorType1Llvm = ::llvm::VectorType::get(i32Type, 4, false);
275  const auto vectorType2Llvm = ::llvm::VectorType::get(halfType, 9, true);
276 
277  // Act
278  const auto vectorType1Jlm = std::dynamic_pointer_cast<const FixedVectorType>(
279  typeConverter.ConvertLlvmType(*vectorType1Llvm));
280  const auto vectorType2Jlm = std::dynamic_pointer_cast<const ScalableVectorType>(
281  typeConverter.ConvertLlvmType(*vectorType2Llvm));
282 
283  // Assert
284  EXPECT_NE(vectorType1Jlm, nullptr);
285  EXPECT_TRUE(is<BitType>(vectorType1Jlm->type()));
286  EXPECT_EQ(vectorType1Jlm->size(), 4u);
287 
288  EXPECT_NE(vectorType2Jlm, nullptr);
289  EXPECT_TRUE(is<FloatingPointType>(vectorType2Jlm->type()));
290  EXPECT_EQ(vectorType2Jlm->size(), 9u);
291 }
292 
293 TEST(TypeConverterTests, JLmBitTypeConversion)
294 {
295  using namespace jlm::llvm;
296 
297  // Arrange
298  llvm::LLVMContext context;
299  TypeConverter typeConverter;
300 
301  const auto i1 = jlm::rvsdg::BitType::Create(1);
302  const auto i2 = jlm::rvsdg::BitType::Create(2);
303  const auto i4 = jlm::rvsdg::BitType::Create(4);
304  const auto i8 = jlm::rvsdg::BitType::Create(8);
305  const auto i16 = jlm::rvsdg::BitType::Create(16);
306  const auto i32 = jlm::rvsdg::BitType::Create(32);
307  const auto i64 = jlm::rvsdg::BitType::Create(64);
308 
309  // Act
310  const auto i1Type = typeConverter.ConvertJlmType(*i1, context);
311  const auto i2Type = typeConverter.ConvertJlmType(*i2, context);
312  const auto i4Type = typeConverter.ConvertJlmType(*i4, context);
313  const auto i8Type = typeConverter.ConvertJlmType(*i8, context);
314  const auto i16Type = typeConverter.ConvertJlmType(*i16, context);
315  const auto i32Type = typeConverter.ConvertJlmType(*i32, context);
316  const auto i64Type = typeConverter.ConvertJlmType(*i64, context);
317 
318  // Assert
319  EXPECT_EQ(i1Type->getTypeID(), llvm::Type::IntegerTyID);
320  EXPECT_EQ(i1Type->getIntegerBitWidth(), 1u);
321 
322  EXPECT_EQ(i2Type->getTypeID(), llvm::Type::IntegerTyID);
323  EXPECT_EQ(i2Type->getIntegerBitWidth(), 2u);
324 
325  EXPECT_EQ(i4Type->getTypeID(), llvm::Type::IntegerTyID);
326  EXPECT_EQ(i4Type->getIntegerBitWidth(), 4u);
327 
328  EXPECT_EQ(i8Type->getTypeID(), llvm::Type::IntegerTyID);
329  EXPECT_EQ(i8Type->getIntegerBitWidth(), 8u);
330 
331  EXPECT_EQ(i16Type->getTypeID(), llvm::Type::IntegerTyID);
332  EXPECT_EQ(i16Type->getIntegerBitWidth(), 16u);
333 
334  EXPECT_EQ(i32Type->getTypeID(), llvm::Type::IntegerTyID);
335  EXPECT_EQ(i32Type->getIntegerBitWidth(), 32u);
336 
337  EXPECT_EQ(i64Type->getTypeID(), llvm::Type::IntegerTyID);
338  EXPECT_EQ(i64Type->getIntegerBitWidth(), 64u);
339 }
340 
341 TEST(TypeConverterTests, JlmFunctionTypeConversion)
342 {
343  using namespace jlm::llvm;
344  using namespace jlm::rvsdg;
345 
346  // Arrange
347  llvm::LLVMContext context;
348  TypeConverter typeConverter;
349 
350  auto bit32Type = BitType::Create(32);
351  auto ioStateType = IOStateType::Create();
352  auto memoryStateType = MemoryStateType::Create();
353  auto varArgType = VariableArgumentType::Create();
354  const auto functionType1Jlm = FunctionType::Create(
355  { bit32Type, bit32Type, ioStateType, memoryStateType },
356  { memoryStateType, ioStateType });
357  const auto functionType2Jlm = FunctionType::Create(
358  { ioStateType, memoryStateType },
359  { bit32Type, memoryStateType, ioStateType });
360  const auto functionType3Jlm = FunctionType::Create(
361  { bit32Type, bit32Type, varArgType, ioStateType, memoryStateType },
362  { bit32Type, memoryStateType, ioStateType });
363 
364  // Act
365  const auto functionType1Llvm =
366  llvm::dyn_cast<llvm::FunctionType>(typeConverter.ConvertJlmType(*functionType1Jlm, context));
367  const auto functionType2Llvm =
368  llvm::dyn_cast<llvm::FunctionType>(typeConverter.ConvertJlmType(*functionType2Jlm, context));
369  const auto functionType3Llvm =
370  llvm::dyn_cast<llvm::FunctionType>(typeConverter.ConvertJlmType(*functionType3Jlm, context));
371 
372  // Assert
373  EXPECT_NE(functionType1Llvm, nullptr);
374  EXPECT_EQ(functionType1Llvm->getNumParams(), 2u);
375  EXPECT_EQ(functionType1Llvm->getParamType(0)->getTypeID(), llvm::Type::IntegerTyID);
376  EXPECT_EQ(functionType1Llvm->getParamType(1)->getTypeID(), llvm::Type::IntegerTyID);
377  EXPECT_EQ(functionType1Llvm->getReturnType()->getTypeID(), llvm::Type::VoidTyID);
378  EXPECT_FALSE(functionType1Llvm->isVarArg());
379 
380  EXPECT_NE(functionType2Llvm, nullptr);
381  EXPECT_EQ(functionType2Llvm->getNumParams(), 0u);
382  EXPECT_EQ(functionType2Llvm->getReturnType()->getTypeID(), llvm::Type::IntegerTyID);
383  EXPECT_FALSE(functionType2Llvm->isVarArg());
384 
385  EXPECT_NE(functionType3Llvm, nullptr);
386  EXPECT_EQ(functionType3Llvm->getNumParams(), 2u);
387  EXPECT_EQ(functionType3Llvm->getParamType(0)->getTypeID(), llvm::Type::IntegerTyID);
388  EXPECT_EQ(functionType3Llvm->getParamType(1)->getTypeID(), llvm::Type::IntegerTyID);
389  EXPECT_EQ(functionType3Llvm->getReturnType()->getTypeID(), llvm::Type::IntegerTyID);
390  EXPECT_TRUE(functionType3Llvm->isVarArg());
391 }
392 
393 TEST(TypeConverterTests, JlmPointerTypeConversion)
394 {
395  using namespace jlm::llvm;
396 
397  // Arrange
398  llvm::LLVMContext context;
399  TypeConverter typeConverter;
400 
401  const auto pointerTypeJlm = PointerType::Create();
402 
403  // Act
404  const auto pointerTypeLlvm =
405  llvm::dyn_cast<llvm::PointerType>(typeConverter.ConvertJlmType(*pointerTypeJlm, context));
406 
407  // Assert
408  EXPECT_NE(pointerTypeLlvm, nullptr);
409  EXPECT_EQ(pointerTypeLlvm->getAddressSpace(), 0u);
410 }
411 
412 TEST(TypeConverterTests, JlmArrayTypeConversion)
413 {
414  using namespace jlm::llvm;
415  using namespace jlm::rvsdg;
416 
417  // Arrange
418  llvm::LLVMContext context;
419  TypeConverter typeConverter;
420 
421  const auto bit32Type = BitType::Create(32);
422  const auto halfType = FloatingPointType::Create(fpsize::half);
423  const auto arrayType1Jlm = ArrayType::Create(bit32Type, 4);
424  const auto arrayType2Jlm = ArrayType::Create(halfType, 9);
425 
426  // Act
427  const auto arrayType1Llvm = typeConverter.ConvertJlmType(*arrayType1Jlm, context);
428  const auto arrayType2Llvm = typeConverter.ConvertJlmType(*arrayType2Jlm, context);
429 
430  // Assert
431  EXPECT_TRUE(arrayType1Llvm->isArrayTy());
432  EXPECT_EQ(arrayType1Llvm->getArrayNumElements(), 4u);
433  EXPECT_EQ(arrayType1Llvm->getArrayElementType()->getTypeID(), llvm::Type::IntegerTyID);
434 
435  EXPECT_TRUE(arrayType2Llvm->isArrayTy());
436  EXPECT_EQ(arrayType2Llvm->getArrayNumElements(), 9u);
437  EXPECT_EQ(arrayType2Llvm->getArrayElementType()->getTypeID(), llvm::Type::HalfTyID);
438 }
439 
440 TEST(TypeConverterTests, JlmControlTypeConversion)
441 {
442  using namespace jlm::llvm;
443 
444  // Arrange
445  llvm::LLVMContext context;
446  TypeConverter typeConverter;
447 
448  const auto controlType1 = jlm::rvsdg::ControlType::Create(2);
449  const auto controlType10 = jlm::rvsdg::ControlType::Create(10);
450 
451  // Act
452  const auto integerType1Llvm = typeConverter.ConvertJlmType(*controlType1, context);
453  const auto integerType2Llvm = typeConverter.ConvertJlmType(*controlType10, context);
454 
455  // Assert
456  EXPECT_EQ(integerType1Llvm->getTypeID(), llvm::Type::IntegerTyID);
457  EXPECT_EQ(integerType1Llvm->getIntegerBitWidth(), 1u);
458 
459  EXPECT_EQ(integerType2Llvm->getTypeID(), llvm::Type::IntegerTyID);
460  EXPECT_EQ(integerType2Llvm->getIntegerBitWidth(), 32u);
461 }
462 
463 TEST(TypeConverterTests, JlmFloatingPointTypeConversion)
464 {
465  using namespace jlm::llvm;
466 
467  // Arrange
468  llvm::LLVMContext context;
469  TypeConverter typeConverter;
470 
471  const auto halfTypeJlm = FloatingPointType::Create(fpsize::half);
472  const auto floatTypeJlm = FloatingPointType::Create(fpsize::flt);
473  const auto doubleTypeJlm = FloatingPointType::Create(fpsize::dbl);
474  const auto x86fp80TypeJlm = FloatingPointType::Create(fpsize::x86fp80);
475  const auto fp128TypeJlm = FloatingPointType::Create(fpsize::fp128);
476 
477  // Act
478  const auto halfTypeLlvm = typeConverter.ConvertJlmType(*halfTypeJlm, context);
479  const auto floatTypeLlvm = typeConverter.ConvertJlmType(*floatTypeJlm, context);
480  const auto doubleTypeLlvm = typeConverter.ConvertJlmType(*doubleTypeJlm, context);
481  const auto x86fp80TypeLlvm = typeConverter.ConvertJlmType(*x86fp80TypeJlm, context);
482  const auto fp128TypeLlvm = typeConverter.ConvertJlmType(*fp128TypeJlm, context);
483 
484  // Assert
485  EXPECT_EQ(halfTypeLlvm->getTypeID(), llvm::Type::HalfTyID);
486  EXPECT_EQ(floatTypeLlvm->getTypeID(), llvm::Type::FloatTyID);
487  EXPECT_EQ(doubleTypeLlvm->getTypeID(), llvm::Type::DoubleTyID);
488  EXPECT_EQ(x86fp80TypeLlvm->getTypeID(), llvm::Type::X86_FP80TyID);
489  EXPECT_EQ(fp128TypeLlvm->getTypeID(), llvm::Type::FP128TyID);
490 }
491 
492 TEST(TypeConverterTests, JlmStructTypeConversion)
493 {
494  using namespace jlm::llvm;
495 
496  // Arrange
497  llvm::LLVMContext context;
498  TypeConverter typeConverter;
499 
500  const auto bit32Type = jlm::rvsdg::BitType::Create(32);
501  const auto halfType = FloatingPointType::Create(fpsize::half);
502 
503  const auto structType1Jlm = StructType::CreateIdentified({ bit32Type, halfType }, false);
504  const auto structType2Jlm =
505  StructType::CreateIdentified({ bit32Type, bit32Type, bit32Type }, false);
506  const auto structType3Jlm = StructType::CreateIdentified("myStruct", { bit32Type }, true);
507 
508  // Act
509  const auto structType1Llvm = typeConverter.ConvertJlmType(*structType1Jlm, context);
510  const auto structType2Llvm = typeConverter.ConvertJlmType(*structType2Jlm, context);
511  const auto structType3Llvm = typeConverter.ConvertJlmType(*structType3Jlm, context);
512 
513  const auto structType4Llvm = typeConverter.ConvertJlmType(*structType1Jlm, context);
514 
515  // Assert
516  EXPECT_EQ(structType1Llvm->getTypeID(), llvm::Type::StructTyID);
517  EXPECT_EQ(structType1Llvm->getStructNumElements(), 2u);
518  EXPECT_EQ(structType1Llvm->getStructElementType(0)->getTypeID(), llvm::Type::IntegerTyID);
519  EXPECT_EQ(structType1Llvm->getStructElementType(1)->getTypeID(), llvm::Type::HalfTyID);
520  EXPECT_FALSE(llvm::dyn_cast<llvm::StructType>(structType1Llvm)->isLiteral());
521  EXPECT_FALSE(llvm::dyn_cast<llvm::StructType>(structType1Llvm)->isPacked());
522 
523  EXPECT_EQ(structType2Llvm->getTypeID(), llvm::Type::StructTyID);
524  EXPECT_EQ(structType2Llvm->getStructNumElements(), 3u);
525  EXPECT_EQ(structType2Llvm->getStructElementType(0)->getTypeID(), llvm::Type::IntegerTyID);
526  EXPECT_EQ(structType2Llvm->getStructElementType(1)->getTypeID(), llvm::Type::IntegerTyID);
527  EXPECT_EQ(structType2Llvm->getStructElementType(2)->getTypeID(), llvm::Type::IntegerTyID);
528  EXPECT_FALSE(llvm::dyn_cast<llvm::StructType>(structType2Llvm)->isLiteral());
529  EXPECT_FALSE(llvm::dyn_cast<llvm::StructType>(structType2Llvm)->isPacked());
530 
531  EXPECT_EQ(structType3Llvm->getTypeID(), llvm::Type::StructTyID);
532  EXPECT_EQ(structType3Llvm->getStructNumElements(), 1u);
533  EXPECT_EQ(structType3Llvm->getStructElementType(0)->getTypeID(), llvm::Type::IntegerTyID);
534  EXPECT_EQ(structType3Llvm->getStructName(), "myStruct");
535  EXPECT_FALSE(llvm::dyn_cast<llvm::StructType>(structType3Llvm)->isLiteral());
536  EXPECT_TRUE(llvm::dyn_cast<llvm::StructType>(structType3Llvm)->isPacked());
537 
538  EXPECT_EQ(structType4Llvm, structType1Llvm);
539 }
540 
541 TEST(TypeConverterTests, JlmFixedVectorTypeConversion)
542 {
543  using namespace jlm::llvm;
544  using namespace jlm::rvsdg;
545 
546  // Arrange
547  llvm::LLVMContext context;
548  TypeConverter typeConverter;
549 
550  const auto bit32Type = BitType::Create(32);
551  const auto fixedVectorType1 = FixedVectorType::Create(bit32Type, 2);
552  const auto fixedVectorType2 = FixedVectorType::Create(bit32Type, 4);
553 
554  // Act
555  const auto vectorType1 =
556  llvm::dyn_cast<llvm::VectorType>(typeConverter.ConvertJlmType(*fixedVectorType1, context));
557  const auto vectorType2 =
558  llvm::dyn_cast<llvm::VectorType>(typeConverter.ConvertJlmType(*fixedVectorType2, context));
559 
560  // Assert
561  EXPECT_EQ(vectorType1->getTypeID(), llvm::Type::FixedVectorTyID);
562  EXPECT_EQ(vectorType1->getElementType()->getTypeID(), llvm::Type::IntegerTyID);
563  EXPECT_EQ(vectorType1->getElementCount().getFixedValue(), 2u);
564 
565  EXPECT_EQ(vectorType2->getTypeID(), llvm::Type::FixedVectorTyID);
566  EXPECT_EQ(vectorType2->getElementType()->getTypeID(), llvm::Type::IntegerTyID);
567  EXPECT_EQ(vectorType2->getElementCount().getFixedValue(), 4u);
568 }
569 
570 TEST(TypeConverterTests, JlmScalableVectorTypeConversion)
571 {
572  using namespace jlm::llvm;
573  using namespace jlm::rvsdg;
574 
575  // Arrange
576  llvm::LLVMContext context;
577  TypeConverter typeConverter;
578 
579  const auto bit32Type = BitType::Create(32);
580  const auto scalableVectorType1 = ScalableVectorType::Create(bit32Type, 2);
581  const auto scalableVectorType2 = ScalableVectorType::Create(bit32Type, 4);
582 
583  // Act
584  const auto vectorType1 =
585  llvm::dyn_cast<llvm::VectorType>(typeConverter.ConvertJlmType(*scalableVectorType1, context));
586  const auto vectorType2 =
587  llvm::dyn_cast<llvm::VectorType>(typeConverter.ConvertJlmType(*scalableVectorType2, context));
588 
589  // Assert
590  EXPECT_EQ(vectorType1->getTypeID(), llvm::Type::ScalableVectorTyID);
591  EXPECT_EQ(vectorType1->getElementType()->getTypeID(), llvm::Type::IntegerTyID);
592  EXPECT_EQ(vectorType1->getElementCount().getKnownMinValue(), 2u);
593 
594  EXPECT_EQ(vectorType2->getTypeID(), llvm::Type::ScalableVectorTyID);
595  EXPECT_EQ(vectorType2->getElementType()->getTypeID(), llvm::Type::IntegerTyID);
596  EXPECT_EQ(vectorType2->getElementCount().getKnownMinValue(), 4u);
597 }
TEST(TypeConverterTests, LlvmIntegerTypeConversion)
static std::shared_ptr< const ArrayType > Create(std::shared_ptr< const Type > type, size_t nelements)
Definition: types.hpp:98
static std::shared_ptr< const FixedVectorType > Create(std::shared_ptr< const rvsdg::Type > type, size_t size)
Definition: types.hpp:413
static std::shared_ptr< const FloatingPointType > Create(fpsize size)
Definition: types.cpp:117
static std::shared_ptr< const IOStateType > Create()
Definition: types.cpp:343
static std::shared_ptr< const MemoryStateType > Create()
Definition: types.cpp:379
static std::shared_ptr< const PointerType > Create()
Definition: types.cpp:45
static std::shared_ptr< const ScalableVectorType > Create(std::shared_ptr< const rvsdg::Type > type, size_t size)
Definition: types.hpp:438
static std::shared_ptr< const StructType > CreateIdentified(const std::string &name, std::vector< std::shared_ptr< const Type >> types, bool isPacked)
Definition: types.hpp:307
std::shared_ptr< const rvsdg::Type > ConvertLlvmType(::llvm::Type &type)
::llvm::Type * ConvertJlmType(const rvsdg::Type &type, ::llvm::LLVMContext &context)
static std::shared_ptr< const VariableArgumentType > Create()
Definition: types.cpp:180
static std::shared_ptr< const BitType > Create(std::size_t nbits)
Creates bit type of specified width.
Definition: type.cpp:45
static std::shared_ptr< const ControlType > Create(std::size_t nalternatives)
Instantiates control type.
Definition: control.cpp:50
Global memory state passed between functions.