xref: /llvm-project/mlir/test/CAPI/llvm.c (revision 2918e779a9545a66c0031b03b3af5bf4d8517cec)
135d4593eSAdam Paszke //===- llvm.c - Test of llvm APIs -----------------------------------------===//
235d4593eSAdam Paszke //
335d4593eSAdam Paszke // Part of the LLVM Project, under the Apache License v2.0 with LLVM
435d4593eSAdam Paszke // Exceptions.
535d4593eSAdam Paszke // See https://llvm.org/LICENSE.txt for license information.
635d4593eSAdam Paszke // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
735d4593eSAdam Paszke //
835d4593eSAdam Paszke //===----------------------------------------------------------------------===//
935d4593eSAdam Paszke 
1035d4593eSAdam Paszke // RUN: mlir-capi-llvm-test 2>&1 | FileCheck %s
1135d4593eSAdam Paszke 
1235d4593eSAdam Paszke #include "mlir-c/Dialect/LLVM.h"
133714f937SEdgar #include "mlir-c/BuiltinAttributes.h"
1435d4593eSAdam Paszke #include "mlir-c/BuiltinTypes.h"
1546edbce4SChristian Ulmann #include "mlir-c/IR.h"
16bd8fcf75SOleksandr "Alex" Zinenko #include "mlir-c/Support.h"
173714f937SEdgar #include "llvm-c/Core.h"
183714f937SEdgar #include "llvm-c/DebugInfo.h"
1935d4593eSAdam Paszke 
2035d4593eSAdam Paszke #include <assert.h>
21d9e4309bSMarkus Böck #include <inttypes.h>
2235d4593eSAdam Paszke #include <math.h>
2335d4593eSAdam Paszke #include <stdio.h>
2435d4593eSAdam Paszke #include <stdlib.h>
2535d4593eSAdam Paszke #include <string.h>
2635d4593eSAdam Paszke 
2735d4593eSAdam Paszke // CHECK-LABEL: testTypeCreation()
2835d4593eSAdam Paszke static void testTypeCreation(MlirContext ctx) {
2935d4593eSAdam Paszke   fprintf(stderr, "testTypeCreation()\n");
308a2720d8SAdam Paszke   MlirType i8 = mlirIntegerTypeGet(ctx, 8);
3135d4593eSAdam Paszke   MlirType i32 = mlirIntegerTypeGet(ctx, 32);
328a2720d8SAdam Paszke   MlirType i64 = mlirIntegerTypeGet(ctx, 64);
3335d4593eSAdam Paszke 
3446edbce4SChristian Ulmann   const char *ptr_text = "!llvm.ptr";
3546edbce4SChristian Ulmann   MlirType ptr = mlirLLVMPointerTypeGet(ctx, 0);
3646edbce4SChristian Ulmann   MlirType ptr_ref =
3746edbce4SChristian Ulmann       mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(ptr_text));
3846edbce4SChristian Ulmann   // CHECK: !llvm.ptr: 1
3946edbce4SChristian Ulmann   fprintf(stderr, "%s: %d\n", ptr_text, mlirTypeEqual(ptr, ptr_ref));
4035d4593eSAdam Paszke 
4146edbce4SChristian Ulmann   const char *ptr_addr_text = "!llvm.ptr<42>";
4246edbce4SChristian Ulmann   MlirType ptr_addr = mlirLLVMPointerTypeGet(ctx, 42);
4346edbce4SChristian Ulmann   MlirType ptr_addr_ref =
4446edbce4SChristian Ulmann       mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(ptr_addr_text));
4546edbce4SChristian Ulmann   // CHECK: !llvm.ptr<42>: 1
4646edbce4SChristian Ulmann   fprintf(stderr, "%s: %d\n", ptr_addr_text,
4746edbce4SChristian Ulmann           mlirTypeEqual(ptr_addr, ptr_addr_ref));
488a2720d8SAdam Paszke 
498a2720d8SAdam Paszke   const char *voidt_text = "!llvm.void";
508a2720d8SAdam Paszke   MlirType voidt = mlirLLVMVoidTypeGet(ctx);
518a2720d8SAdam Paszke   MlirType voidt_ref =
528a2720d8SAdam Paszke       mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(voidt_text));
538a2720d8SAdam Paszke   // CHECK: !llvm.void: 1
548a2720d8SAdam Paszke   fprintf(stderr, "%s: %d\n", voidt_text, mlirTypeEqual(voidt, voidt_ref));
558a2720d8SAdam Paszke 
568a2720d8SAdam Paszke   const char *i32_4_text = "!llvm.array<4 x i32>";
578a2720d8SAdam Paszke   MlirType i32_4 = mlirLLVMArrayTypeGet(i32, 4);
588a2720d8SAdam Paszke   MlirType i32_4_ref =
598a2720d8SAdam Paszke       mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(i32_4_text));
608a2720d8SAdam Paszke   // CHECK: !llvm.array<4 x i32>: 1
618a2720d8SAdam Paszke   fprintf(stderr, "%s: %d\n", i32_4_text, mlirTypeEqual(i32_4, i32_4_ref));
628a2720d8SAdam Paszke 
638a2720d8SAdam Paszke   const char *i8_i32_i64_text = "!llvm.func<i8 (i32, i64)>";
648a2720d8SAdam Paszke   const MlirType i32_i64_arr[] = {i32, i64};
658a2720d8SAdam Paszke   MlirType i8_i32_i64 = mlirLLVMFunctionTypeGet(i8, 2, i32_i64_arr, false);
668a2720d8SAdam Paszke   MlirType i8_i32_i64_ref =
678a2720d8SAdam Paszke       mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(i8_i32_i64_text));
688a2720d8SAdam Paszke   // CHECK: !llvm.func<i8 (i32, i64)>: 1
698a2720d8SAdam Paszke   fprintf(stderr, "%s: %d\n", i8_i32_i64_text,
708a2720d8SAdam Paszke           mlirTypeEqual(i8_i32_i64, i8_i32_i64_ref));
718a2720d8SAdam Paszke 
728a2720d8SAdam Paszke   const char *i32_i64_s_text = "!llvm.struct<(i32, i64)>";
738a2720d8SAdam Paszke   MlirType i32_i64_s = mlirLLVMStructTypeLiteralGet(ctx, 2, i32_i64_arr, false);
748a2720d8SAdam Paszke   MlirType i32_i64_s_ref =
758a2720d8SAdam Paszke       mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(i32_i64_s_text));
768a2720d8SAdam Paszke   // CHECK: !llvm.struct<(i32, i64)>: 1
778a2720d8SAdam Paszke   fprintf(stderr, "%s: %d\n", i32_i64_s_text,
788a2720d8SAdam Paszke           mlirTypeEqual(i32_i64_s, i32_i64_s_ref));
7935d4593eSAdam Paszke }
8035d4593eSAdam Paszke 
81bd8fcf75SOleksandr "Alex" Zinenko // CHECK-LABEL: testStructTypeCreation
82bd8fcf75SOleksandr "Alex" Zinenko static int testStructTypeCreation(MlirContext ctx) {
833714f937SEdgar   fprintf(stderr, "testStructTypeCreation\n");
84bd8fcf75SOleksandr "Alex" Zinenko 
85bd8fcf75SOleksandr "Alex" Zinenko   // CHECK: !llvm.struct<()>
86bd8fcf75SOleksandr "Alex" Zinenko   mlirTypeDump(mlirLLVMStructTypeLiteralGet(ctx, /*nFieldTypes=*/0,
87bd8fcf75SOleksandr "Alex" Zinenko                                             /*fieldTypes=*/NULL,
88bd8fcf75SOleksandr "Alex" Zinenko                                             /*isPacked=*/false));
89bd8fcf75SOleksandr "Alex" Zinenko 
90bd8fcf75SOleksandr "Alex" Zinenko   MlirType i8 = mlirIntegerTypeGet(ctx, 8);
91bd8fcf75SOleksandr "Alex" Zinenko   MlirType i32 = mlirIntegerTypeGet(ctx, 32);
92bd8fcf75SOleksandr "Alex" Zinenko   MlirType i64 = mlirIntegerTypeGet(ctx, 64);
93bd8fcf75SOleksandr "Alex" Zinenko   MlirType i8_i32_i64[] = {i8, i32, i64};
94bd8fcf75SOleksandr "Alex" Zinenko   // CHECK: !llvm.struct<(i8, i32, i64)>
95bd8fcf75SOleksandr "Alex" Zinenko   mlirTypeDump(
96bd8fcf75SOleksandr "Alex" Zinenko       mlirLLVMStructTypeLiteralGet(ctx, sizeof(i8_i32_i64) / sizeof(MlirType),
97bd8fcf75SOleksandr "Alex" Zinenko                                    i8_i32_i64, /*isPacked=*/false));
98bd8fcf75SOleksandr "Alex" Zinenko   // CHECK: !llvm.struct<(i32)>
99bd8fcf75SOleksandr "Alex" Zinenko   mlirTypeDump(mlirLLVMStructTypeLiteralGet(ctx, 1, &i32, /*isPacked=*/false));
100bd8fcf75SOleksandr "Alex" Zinenko   MlirType i32_i32[] = {i32, i32};
101bd8fcf75SOleksandr "Alex" Zinenko   // CHECK: !llvm.struct<packed (i32, i32)>
102bd8fcf75SOleksandr "Alex" Zinenko   mlirTypeDump(mlirLLVMStructTypeLiteralGet(
103bd8fcf75SOleksandr "Alex" Zinenko       ctx, sizeof(i32_i32) / sizeof(MlirType), i32_i32, /*isPacked=*/true));
104bd8fcf75SOleksandr "Alex" Zinenko 
105bd8fcf75SOleksandr "Alex" Zinenko   MlirType literal =
106bd8fcf75SOleksandr "Alex" Zinenko       mlirLLVMStructTypeLiteralGet(ctx, sizeof(i8_i32_i64) / sizeof(MlirType),
107bd8fcf75SOleksandr "Alex" Zinenko                                    i8_i32_i64, /*isPacked=*/false);
108bd8fcf75SOleksandr "Alex" Zinenko   // CHECK: num elements: 3
109bd8fcf75SOleksandr "Alex" Zinenko   // CHECK: i8
110bd8fcf75SOleksandr "Alex" Zinenko   // CHECK: i32
111bd8fcf75SOleksandr "Alex" Zinenko   // CHECK: i64
112d9e4309bSMarkus Böck   fprintf(stderr, "num elements: %" PRIdPTR "\n",
113bd8fcf75SOleksandr "Alex" Zinenko           mlirLLVMStructTypeGetNumElementTypes(literal));
114bd8fcf75SOleksandr "Alex" Zinenko   for (intptr_t i = 0; i < 3; ++i) {
115bd8fcf75SOleksandr "Alex" Zinenko     mlirTypeDump(mlirLLVMStructTypeGetElementType(literal, i));
116bd8fcf75SOleksandr "Alex" Zinenko   }
117bd8fcf75SOleksandr "Alex" Zinenko 
118bd8fcf75SOleksandr "Alex" Zinenko   if (!mlirTypeEqual(
119bd8fcf75SOleksandr "Alex" Zinenko           mlirLLVMStructTypeLiteralGet(ctx, 1, &i32, /*isPacked=*/false),
120bd8fcf75SOleksandr "Alex" Zinenko           mlirLLVMStructTypeLiteralGet(ctx, 1, &i32, /*isPacked=*/false))) {
121bd8fcf75SOleksandr "Alex" Zinenko     return 1;
122bd8fcf75SOleksandr "Alex" Zinenko   }
123bd8fcf75SOleksandr "Alex" Zinenko   if (mlirTypeEqual(
124bd8fcf75SOleksandr "Alex" Zinenko           mlirLLVMStructTypeLiteralGet(ctx, 1, &i32, /*isPacked=*/false),
125bd8fcf75SOleksandr "Alex" Zinenko           mlirLLVMStructTypeLiteralGet(ctx, 1, &i64, /*isPacked=*/false))) {
126bd8fcf75SOleksandr "Alex" Zinenko     return 2;
127bd8fcf75SOleksandr "Alex" Zinenko   }
128bd8fcf75SOleksandr "Alex" Zinenko 
129bd8fcf75SOleksandr "Alex" Zinenko   // CHECK: !llvm.struct<"foo", opaque>
130bd8fcf75SOleksandr "Alex" Zinenko   // CHECK: !llvm.struct<"bar", opaque>
131bd8fcf75SOleksandr "Alex" Zinenko   mlirTypeDump(mlirLLVMStructTypeIdentifiedGet(
132bd8fcf75SOleksandr "Alex" Zinenko       ctx, mlirStringRefCreateFromCString("foo")));
133bd8fcf75SOleksandr "Alex" Zinenko   mlirTypeDump(mlirLLVMStructTypeIdentifiedGet(
134bd8fcf75SOleksandr "Alex" Zinenko       ctx, mlirStringRefCreateFromCString("bar")));
135bd8fcf75SOleksandr "Alex" Zinenko 
136bd8fcf75SOleksandr "Alex" Zinenko   if (!mlirTypeEqual(mlirLLVMStructTypeIdentifiedGet(
137bd8fcf75SOleksandr "Alex" Zinenko                          ctx, mlirStringRefCreateFromCString("foo")),
138bd8fcf75SOleksandr "Alex" Zinenko                      mlirLLVMStructTypeIdentifiedGet(
139bd8fcf75SOleksandr "Alex" Zinenko                          ctx, mlirStringRefCreateFromCString("foo")))) {
140bd8fcf75SOleksandr "Alex" Zinenko     return 3;
141bd8fcf75SOleksandr "Alex" Zinenko   }
142bd8fcf75SOleksandr "Alex" Zinenko   if (mlirTypeEqual(mlirLLVMStructTypeIdentifiedGet(
143bd8fcf75SOleksandr "Alex" Zinenko                         ctx, mlirStringRefCreateFromCString("foo")),
144bd8fcf75SOleksandr "Alex" Zinenko                     mlirLLVMStructTypeIdentifiedGet(
145bd8fcf75SOleksandr "Alex" Zinenko                         ctx, mlirStringRefCreateFromCString("bar")))) {
146bd8fcf75SOleksandr "Alex" Zinenko     return 4;
147bd8fcf75SOleksandr "Alex" Zinenko   }
148bd8fcf75SOleksandr "Alex" Zinenko 
149bd8fcf75SOleksandr "Alex" Zinenko   MlirType fooStruct = mlirLLVMStructTypeIdentifiedGet(
150bd8fcf75SOleksandr "Alex" Zinenko       ctx, mlirStringRefCreateFromCString("foo"));
151bd8fcf75SOleksandr "Alex" Zinenko   MlirStringRef name = mlirLLVMStructTypeGetIdentifier(fooStruct);
152bd8fcf75SOleksandr "Alex" Zinenko   if (memcmp(name.data, "foo", name.length))
153bd8fcf75SOleksandr "Alex" Zinenko     return 5;
154bd8fcf75SOleksandr "Alex" Zinenko   if (!mlirLLVMStructTypeIsOpaque(fooStruct))
155bd8fcf75SOleksandr "Alex" Zinenko     return 6;
156bd8fcf75SOleksandr "Alex" Zinenko 
157bd8fcf75SOleksandr "Alex" Zinenko   MlirType i32_i64[] = {i32, i64};
158bd8fcf75SOleksandr "Alex" Zinenko   MlirLogicalResult result =
159bd8fcf75SOleksandr "Alex" Zinenko       mlirLLVMStructTypeSetBody(fooStruct, sizeof(i32_i64) / sizeof(MlirType),
160bd8fcf75SOleksandr "Alex" Zinenko                                 i32_i64, /*isPacked=*/false);
161bd8fcf75SOleksandr "Alex" Zinenko   if (!mlirLogicalResultIsSuccess(result))
162bd8fcf75SOleksandr "Alex" Zinenko     return 7;
163bd8fcf75SOleksandr "Alex" Zinenko 
164bd8fcf75SOleksandr "Alex" Zinenko   // CHECK: !llvm.struct<"foo", (i32, i64)>
165bd8fcf75SOleksandr "Alex" Zinenko   mlirTypeDump(fooStruct);
166bd8fcf75SOleksandr "Alex" Zinenko   if (mlirLLVMStructTypeIsOpaque(fooStruct))
167bd8fcf75SOleksandr "Alex" Zinenko     return 8;
168bd8fcf75SOleksandr "Alex" Zinenko   if (mlirLLVMStructTypeIsPacked(fooStruct))
169bd8fcf75SOleksandr "Alex" Zinenko     return 9;
170bd8fcf75SOleksandr "Alex" Zinenko   if (!mlirTypeEqual(mlirLLVMStructTypeIdentifiedGet(
171bd8fcf75SOleksandr "Alex" Zinenko                          ctx, mlirStringRefCreateFromCString("foo")),
172bd8fcf75SOleksandr "Alex" Zinenko                      fooStruct)) {
173bd8fcf75SOleksandr "Alex" Zinenko     return 10;
174bd8fcf75SOleksandr "Alex" Zinenko   }
175bd8fcf75SOleksandr "Alex" Zinenko 
176bd8fcf75SOleksandr "Alex" Zinenko   MlirType barStruct = mlirLLVMStructTypeIdentifiedGet(
177bd8fcf75SOleksandr "Alex" Zinenko       ctx, mlirStringRefCreateFromCString("bar"));
178bd8fcf75SOleksandr "Alex" Zinenko   result = mlirLLVMStructTypeSetBody(barStruct, 1, &i32, /*isPacked=*/true);
179bd8fcf75SOleksandr "Alex" Zinenko   if (!mlirLogicalResultIsSuccess(result))
180bd8fcf75SOleksandr "Alex" Zinenko     return 11;
181bd8fcf75SOleksandr "Alex" Zinenko 
182bd8fcf75SOleksandr "Alex" Zinenko   // CHECK: !llvm.struct<"bar", packed (i32)>
183bd8fcf75SOleksandr "Alex" Zinenko   mlirTypeDump(barStruct);
184bd8fcf75SOleksandr "Alex" Zinenko   if (!mlirLLVMStructTypeIsPacked(barStruct))
185bd8fcf75SOleksandr "Alex" Zinenko     return 12;
186bd8fcf75SOleksandr "Alex" Zinenko 
187bd8fcf75SOleksandr "Alex" Zinenko   // Same body, should succeed.
188bd8fcf75SOleksandr "Alex" Zinenko   result =
189bd8fcf75SOleksandr "Alex" Zinenko       mlirLLVMStructTypeSetBody(fooStruct, sizeof(i32_i64) / sizeof(MlirType),
190bd8fcf75SOleksandr "Alex" Zinenko                                 i32_i64, /*isPacked=*/false);
191bd8fcf75SOleksandr "Alex" Zinenko   if (!mlirLogicalResultIsSuccess(result))
192bd8fcf75SOleksandr "Alex" Zinenko     return 13;
193bd8fcf75SOleksandr "Alex" Zinenko 
194bd8fcf75SOleksandr "Alex" Zinenko   // Different body, should fail.
195bd8fcf75SOleksandr "Alex" Zinenko   result = mlirLLVMStructTypeSetBody(fooStruct, 1, &i32, /*isPacked=*/false);
196bd8fcf75SOleksandr "Alex" Zinenko   if (mlirLogicalResultIsSuccess(result))
197bd8fcf75SOleksandr "Alex" Zinenko     return 14;
198bd8fcf75SOleksandr "Alex" Zinenko 
199bd8fcf75SOleksandr "Alex" Zinenko   // Packed flag differs, should fail.
200bd8fcf75SOleksandr "Alex" Zinenko   result = mlirLLVMStructTypeSetBody(barStruct, 1, &i32, /*isPacked=*/false);
201bd8fcf75SOleksandr "Alex" Zinenko   if (mlirLogicalResultIsSuccess(result))
202bd8fcf75SOleksandr "Alex" Zinenko     return 15;
203bd8fcf75SOleksandr "Alex" Zinenko 
204bd8fcf75SOleksandr "Alex" Zinenko   // Should have a different name.
205bd8fcf75SOleksandr "Alex" Zinenko   // CHECK: !llvm.struct<"foo{{[^"]+}}
206bd8fcf75SOleksandr "Alex" Zinenko   mlirTypeDump(mlirLLVMStructTypeIdentifiedNewGet(
207bd8fcf75SOleksandr "Alex" Zinenko       ctx, mlirStringRefCreateFromCString("foo"), /*nFieldTypes=*/0,
208bd8fcf75SOleksandr "Alex" Zinenko       /*fieldTypes=*/NULL, /*isPacked=*/false));
209bd8fcf75SOleksandr "Alex" Zinenko 
210bd8fcf75SOleksandr "Alex" Zinenko   // Two freshly created "new" types must differ.
211bd8fcf75SOleksandr "Alex" Zinenko   if (mlirTypeEqual(
212bd8fcf75SOleksandr "Alex" Zinenko           mlirLLVMStructTypeIdentifiedNewGet(
213bd8fcf75SOleksandr "Alex" Zinenko               ctx, mlirStringRefCreateFromCString("foo"), /*nFieldTypes=*/0,
214bd8fcf75SOleksandr "Alex" Zinenko               /*fieldTypes=*/NULL, /*isPacked=*/false),
215bd8fcf75SOleksandr "Alex" Zinenko           mlirLLVMStructTypeIdentifiedNewGet(
216bd8fcf75SOleksandr "Alex" Zinenko               ctx, mlirStringRefCreateFromCString("foo"), /*nFieldTypes=*/0,
217bd8fcf75SOleksandr "Alex" Zinenko               /*fieldTypes=*/NULL, /*isPacked=*/false))) {
218bd8fcf75SOleksandr "Alex" Zinenko     return 16;
219bd8fcf75SOleksandr "Alex" Zinenko   }
220bd8fcf75SOleksandr "Alex" Zinenko 
221bd8fcf75SOleksandr "Alex" Zinenko   MlirType opaque = mlirLLVMStructTypeOpaqueGet(
222bd8fcf75SOleksandr "Alex" Zinenko       ctx, mlirStringRefCreateFromCString("opaque"));
223bd8fcf75SOleksandr "Alex" Zinenko   // CHECK: !llvm.struct<"opaque", opaque>
224bd8fcf75SOleksandr "Alex" Zinenko   mlirTypeDump(opaque);
225bd8fcf75SOleksandr "Alex" Zinenko   if (!mlirLLVMStructTypeIsOpaque(opaque))
226bd8fcf75SOleksandr "Alex" Zinenko     return 17;
227bd8fcf75SOleksandr "Alex" Zinenko 
228bd8fcf75SOleksandr "Alex" Zinenko   return 0;
229bd8fcf75SOleksandr "Alex" Zinenko }
230bd8fcf75SOleksandr "Alex" Zinenko 
2313714f937SEdgar // CHECK-LABEL: testLLVMAttributes
2323714f937SEdgar static void testLLVMAttributes(MlirContext ctx) {
2333714f937SEdgar   fprintf(stderr, "testLLVMAttributes\n");
2343714f937SEdgar 
2353714f937SEdgar   // CHECK: #llvm.linkage<internal>
2363714f937SEdgar   mlirAttributeDump(mlirLLVMLinkageAttrGet(ctx, MlirLLVMLinkageInternal));
2373714f937SEdgar   // CHECK: #llvm.cconv<ccc>
2383714f937SEdgar   mlirAttributeDump(mlirLLVMCConvAttrGet(ctx, MlirLLVMCConvC));
2393714f937SEdgar   // CHECK: #llvm<comdat any>
2403714f937SEdgar   mlirAttributeDump(mlirLLVMComdatAttrGet(ctx, MlirLLVMComdatAny));
2413714f937SEdgar }
2423714f937SEdgar 
2433714f937SEdgar // CHECK-LABEL: testDebugInfoAttributes
2443714f937SEdgar static void testDebugInfoAttributes(MlirContext ctx) {
2453714f937SEdgar   fprintf(stderr, "testDebugInfoAttributes\n");
2463714f937SEdgar 
2473714f937SEdgar   MlirAttribute foo =
2483714f937SEdgar       mlirStringAttrGet(ctx, mlirStringRefCreateFromCString("foo"));
2493714f937SEdgar   MlirAttribute bar =
2503714f937SEdgar       mlirStringAttrGet(ctx, mlirStringRefCreateFromCString("bar"));
25175197553STobias Gysi 
25275197553STobias Gysi   MlirAttribute none = mlirUnitAttrGet(ctx);
25375197553STobias Gysi   MlirAttribute id = mlirDisctinctAttrCreate(none);
25475197553STobias Gysi   MlirAttribute recId0 = mlirDisctinctAttrCreate(none);
25575197553STobias Gysi   MlirAttribute recId1 = mlirDisctinctAttrCreate(none);
2563714f937SEdgar 
2573714f937SEdgar   // CHECK: #llvm.di_null_type
2583714f937SEdgar   mlirAttributeDump(mlirLLVMDINullTypeAttrGet(ctx));
2593714f937SEdgar 
26075197553STobias Gysi   // CHECK: #llvm.di_basic_type<name = "foo", sizeInBits =
2613714f937SEdgar   // CHECK-SAME: 64, encoding = DW_ATE_signed>
2623714f937SEdgar   MlirAttribute di_type =
2633714f937SEdgar       mlirLLVMDIBasicTypeAttrGet(ctx, 0, foo, 64, MlirLLVMTypeEncodingSigned);
2643714f937SEdgar   mlirAttributeDump(di_type);
2653714f937SEdgar 
2663714f937SEdgar   MlirAttribute file = mlirLLVMDIFileAttrGet(ctx, foo, bar);
2673714f937SEdgar 
2683714f937SEdgar   // CHECK: #llvm.di_file<"foo" in "bar">
2693714f937SEdgar   mlirAttributeDump(file);
2703714f937SEdgar 
2716f633685SBilly Zhu   MlirAttribute compile_unit = mlirLLVMDICompileUnitAttrGet(
2726f633685SBilly Zhu       ctx, id, LLVMDWARFSourceLanguageC99, file, foo, false,
2736f633685SBilly Zhu       MlirLLVMDIEmissionKindFull, MlirLLVMDINameTableKindDefault);
2743714f937SEdgar 
2753714f937SEdgar   // CHECK: #llvm.di_compile_unit<{{.*}}>
2763714f937SEdgar   mlirAttributeDump(compile_unit);
2773714f937SEdgar 
2783714f937SEdgar   MlirAttribute di_module = mlirLLVMDIModuleAttrGet(
2793714f937SEdgar       ctx, file, compile_unit, foo,
2803714f937SEdgar       mlirStringAttrGet(ctx, mlirStringRefCreateFromCString("")), bar, foo, 1,
2813714f937SEdgar       0);
2823714f937SEdgar   // CHECK: #llvm.di_module<{{.*}}>
2833714f937SEdgar   mlirAttributeDump(di_module);
2843714f937SEdgar 
2853714f937SEdgar   // CHECK: #llvm.di_compile_unit<{{.*}}>
2863714f937SEdgar   mlirAttributeDump(mlirLLVMDIModuleAttrGetScope(di_module));
2873714f937SEdgar 
2883714f937SEdgar   // CHECK: 1 : i32
2893714f937SEdgar   mlirAttributeDump(mlirLLVMDIFlagsAttrGet(ctx, 0x1));
2903714f937SEdgar 
2913714f937SEdgar   // CHECK: #llvm.di_lexical_block<{{.*}}>
2923714f937SEdgar   mlirAttributeDump(
2933714f937SEdgar       mlirLLVMDILexicalBlockAttrGet(ctx, compile_unit, file, 1, 2));
2943714f937SEdgar 
2953714f937SEdgar   // CHECK: #llvm.di_lexical_block_file<{{.*}}>
2963714f937SEdgar   mlirAttributeDump(
2973714f937SEdgar       mlirLLVMDILexicalBlockFileAttrGet(ctx, compile_unit, file, 3));
2983714f937SEdgar 
2993714f937SEdgar   // CHECK: #llvm.di_local_variable<{{.*}}>
3004f320e6aSAbid Qadeer   MlirAttribute local_var = mlirLLVMDILocalVariableAttrGet(
301ef8de68fSWalter Erquinigo       ctx, compile_unit, foo, file, 1, 0, 8, di_type, 0);
3024f320e6aSAbid Qadeer   mlirAttributeDump(local_var);
3033714f937SEdgar   // CHECK: #llvm.di_derived_type<{{.*}}>
3045c35b63dSWilliam G Hatch   // CHECK-NOT: dwarfAddressSpace
3055c35b63dSWilliam G Hatch   mlirAttributeDump(mlirLLVMDIDerivedTypeAttrGet(
3065c35b63dSWilliam G Hatch       ctx, 0, bar, di_type, 64, 8, 0, MLIR_CAPI_DWARF_ADDRESS_SPACE_NULL,
3075c35b63dSWilliam G Hatch       di_type));
3085c35b63dSWilliam G Hatch 
3095c35b63dSWilliam G Hatch   // CHECK: #llvm.di_derived_type<{{.*}} dwarfAddressSpace = 3{{.*}}>
3103714f937SEdgar   mlirAttributeDump(
3115c35b63dSWilliam G Hatch       mlirLLVMDIDerivedTypeAttrGet(ctx, 0, bar, di_type, 64, 8, 0, 3, di_type));
3123714f937SEdgar 
3133714f937SEdgar   MlirAttribute subroutine_type =
3143714f937SEdgar       mlirLLVMDISubroutineTypeAttrGet(ctx, 0x0, 1, &di_type);
3153714f937SEdgar 
3163714f937SEdgar   // CHECK: #llvm.di_subroutine_type<{{.*}}>
3173714f937SEdgar   mlirAttributeDump(subroutine_type);
3183714f937SEdgar 
31975197553STobias Gysi   MlirAttribute di_subprogram_self_rec =
32075197553STobias Gysi       mlirLLVMDISubprogramAttrGetRecSelf(recId0);
321bc4bedd5SAbid Qadeer   MlirAttribute di_imported_entity = mlirLLVMDIImportedEntityAttrGet(
32275197553STobias Gysi       ctx, 0, di_subprogram_self_rec, di_module, file, 1, foo, 1, &local_var);
323bc4bedd5SAbid Qadeer 
324bc4bedd5SAbid Qadeer   mlirAttributeDump(di_imported_entity);
325bc4bedd5SAbid Qadeer   // CHECK: #llvm.di_imported_entity<{{.*}}>
326bc4bedd5SAbid Qadeer 
327*2918e779SWalter Erquinigo   MlirAttribute di_annotation = mlirLLVMDIAnnotationAttrGet(
328*2918e779SWalter Erquinigo       ctx, mlirStringAttrGet(ctx, mlirStringRefCreateFromCString("foo")),
329*2918e779SWalter Erquinigo       mlirStringAttrGet(ctx, mlirStringRefCreateFromCString("bar")));
330*2918e779SWalter Erquinigo 
331*2918e779SWalter Erquinigo   mlirAttributeDump(di_annotation);
332*2918e779SWalter Erquinigo   // CHECK: #llvm.di_annotation<{{.*}}>
333*2918e779SWalter Erquinigo 
334bc4bedd5SAbid Qadeer   MlirAttribute di_subprogram = mlirLLVMDISubprogramAttrGet(
33575197553STobias Gysi       ctx, recId0, false, id, compile_unit, compile_unit, foo, bar, file, 1, 2,
336*2918e779SWalter Erquinigo       0, subroutine_type, 1, &di_imported_entity, 1, &di_annotation);
3373714f937SEdgar   // CHECK: #llvm.di_subprogram<{{.*}}>
3383714f937SEdgar   mlirAttributeDump(di_subprogram);
3393714f937SEdgar 
3403714f937SEdgar   // CHECK: #llvm.di_compile_unit<{{.*}}>
3413714f937SEdgar   mlirAttributeDump(mlirLLVMDISubprogramAttrGetScope(di_subprogram));
3423714f937SEdgar 
3433714f937SEdgar   // CHECK: #llvm.di_file<{{.*}}>
3443714f937SEdgar   mlirAttributeDump(mlirLLVMDISubprogramAttrGetFile(di_subprogram));
3453714f937SEdgar 
3463714f937SEdgar   // CHECK: #llvm.di_subroutine_type<{{.*}}>
3473714f937SEdgar   mlirAttributeDump(mlirLLVMDISubprogramAttrGetType(di_subprogram));
3483714f937SEdgar 
3493714f937SEdgar   MlirAttribute expression_elem =
3503714f937SEdgar       mlirLLVMDIExpressionElemAttrGet(ctx, 1, 1, &(uint64_t){1});
3513714f937SEdgar 
3523714f937SEdgar   // CHECK: #llvm<di_expression_elem(1)>
3533714f937SEdgar   mlirAttributeDump(expression_elem);
3543714f937SEdgar 
355d8038373SAbid Qadeer   MlirAttribute expression =
356d8038373SAbid Qadeer       mlirLLVMDIExpressionAttrGet(ctx, 1, &expression_elem);
3573714f937SEdgar   // CHECK: #llvm.di_expression<[(1)]>
358d8038373SAbid Qadeer   mlirAttributeDump(expression);
359d8038373SAbid Qadeer 
3604f320e6aSAbid Qadeer   MlirAttribute string_type =
3614f320e6aSAbid Qadeer       mlirLLVMDIStringTypeAttrGet(ctx, 0x0, foo, 16, 0, local_var, expression,
3624f320e6aSAbid Qadeer                                   expression, MlirLLVMTypeEncodingSigned);
3634f320e6aSAbid Qadeer   // CHECK: #llvm.di_string_type<{{.*}}>
3644f320e6aSAbid Qadeer   mlirAttributeDump(string_type);
3654f320e6aSAbid Qadeer 
36675197553STobias Gysi   // CHECK: #llvm.di_composite_type<recId = {{.*}}, isRecSelf = true>
36775197553STobias Gysi   mlirAttributeDump(mlirLLVMDICompositeTypeAttrGetRecSelf(recId1));
36875197553STobias Gysi 
369d8038373SAbid Qadeer   // CHECK: #llvm.di_composite_type<{{.*}}>
370d8038373SAbid Qadeer   mlirAttributeDump(mlirLLVMDICompositeTypeAttrGet(
37175197553STobias Gysi       ctx, recId1, false, 0, foo, file, 1, compile_unit, di_type, 0, 64, 8, 1,
37275197553STobias Gysi       &di_type, expression, expression, expression, expression));
3733714f937SEdgar }
3743714f937SEdgar 
3755d91f79fSTom Eccles int main(void) {
37635d4593eSAdam Paszke   MlirContext ctx = mlirContextCreate();
37735d4593eSAdam Paszke   mlirDialectHandleRegisterDialect(mlirGetDialectHandle__llvm__(), ctx);
37835d4593eSAdam Paszke   mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("llvm"));
37935d4593eSAdam Paszke   testTypeCreation(ctx);
380bd8fcf75SOleksandr "Alex" Zinenko   int result = testStructTypeCreation(ctx);
3813714f937SEdgar   testLLVMAttributes(ctx);
3823714f937SEdgar   testDebugInfoAttributes(ctx);
38335d4593eSAdam Paszke   mlirContextDestroy(ctx);
384bd8fcf75SOleksandr "Alex" Zinenko   if (result)
385bd8fcf75SOleksandr "Alex" Zinenko     fprintf(stderr, "FAILED: code %d", result);
386bd8fcf75SOleksandr "Alex" Zinenko   return result;
38735d4593eSAdam Paszke }
388