1 //===- TestDataLayoutQuery.cpp - Test Data Layout Queries -----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "TestDialect.h" 10 #include "mlir/Analysis/DataLayoutAnalysis.h" 11 #include "mlir/Dialect/DLTI/DLTI.h" 12 #include "mlir/IR/BuiltinAttributes.h" 13 #include "mlir/Pass/Pass.h" 14 15 using namespace mlir; 16 17 namespace { 18 19 /// A pass that finds "test.data_layout_query" operations and attaches to them 20 /// attributes containing the results of data layout queries for operation 21 /// result types. 22 struct TestDataLayoutQuery 23 : public PassWrapper<TestDataLayoutQuery, FunctionPass> { 24 void runOnFunction() override { 25 FuncOp func = getFunction(); 26 Builder builder(func.getContext()); 27 const DataLayoutAnalysis &layouts = getAnalysis<DataLayoutAnalysis>(); 28 29 func.walk([&](test::DataLayoutQueryOp op) { 30 // Skip the ops with already processed in a deeper call. 31 if (op->getAttr("size")) 32 return; 33 34 const DataLayout &layout = layouts.getAbove(op); 35 unsigned size = layout.getTypeSize(op.getType()); 36 unsigned bitsize = layout.getTypeSizeInBits(op.getType()); 37 unsigned alignment = layout.getTypeABIAlignment(op.getType()); 38 unsigned preferred = layout.getTypePreferredAlignment(op.getType()); 39 op->setAttrs( 40 {builder.getNamedAttr("size", builder.getIndexAttr(size)), 41 builder.getNamedAttr("bitsize", builder.getIndexAttr(bitsize)), 42 builder.getNamedAttr("alignment", builder.getIndexAttr(alignment)), 43 builder.getNamedAttr("preferred", builder.getIndexAttr(preferred))}); 44 }); 45 } 46 }; 47 } // namespace 48 49 namespace mlir { 50 namespace test { 51 void registerTestDataLayoutQuery() { 52 PassRegistration<TestDataLayoutQuery>("test-data-layout-query", 53 "Test data layout queries"); 54 } 55 } // namespace test 56 } // namespace mlir 57