118 lines · cpp
1//===- OpenACCOpsInterfacesTest.cpp - Unit tests for OpenACC interfaces --===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "mlir/Dialect/MemRef/IR/MemRef.h"10#include "mlir/Dialect/OpenACC/OpenACC.h"11#include "mlir/IR/Builders.h"12#include "mlir/IR/BuiltinTypes.h"13#include "mlir/IR/MLIRContext.h"14#include "mlir/IR/OwningOpRef.h"15#include "gtest/gtest.h"16 17using namespace mlir;18using namespace mlir::acc;19 20//===----------------------------------------------------------------------===//21// Test Fixture22//===----------------------------------------------------------------------===//23 24class OpenACCOpsInterfacesTest : public ::testing::Test {25protected:26 OpenACCOpsInterfacesTest()27 : context(), builder(&context), loc(UnknownLoc::get(&context)) {28 context.loadDialect<acc::OpenACCDialect, memref::MemRefDialect>();29 }30 31 MLIRContext context;32 OpBuilder builder;33 Location loc;34};35 36//===----------------------------------------------------------------------===//37// GlobalVariableOpInterface Tests38//===----------------------------------------------------------------------===//39 40TEST_F(OpenACCOpsInterfacesTest, GlobalVariableOpInterfaceNonConstant) {41 // Test that a non-constant global returns false for isConstant()42 43 auto memrefType = MemRefType::get({10}, builder.getF32Type());44 OwningOpRef<memref::GlobalOp> globalOp = memref::GlobalOp::create(45 builder, loc,46 /*sym_name=*/builder.getStringAttr("mutable_global"),47 /*sym_visibility=*/builder.getStringAttr("private"),48 /*type=*/TypeAttr::get(memrefType),49 /*initial_value=*/Attribute(),50 /*constant=*/UnitAttr(),51 /*alignment=*/IntegerAttr());52 53 auto globalVarIface =54 dyn_cast<GlobalVariableOpInterface>(globalOp->getOperation());55 ASSERT_TRUE(globalVarIface != nullptr);56 EXPECT_FALSE(globalVarIface.isConstant());57}58 59TEST_F(OpenACCOpsInterfacesTest, GlobalVariableOpInterfaceConstant) {60 // Test that a constant global returns true for isConstant()61 62 auto memrefType = MemRefType::get({5}, builder.getI32Type());63 OwningOpRef<memref::GlobalOp> constantGlobalOp = memref::GlobalOp::create(64 builder, loc,65 /*sym_name=*/builder.getStringAttr("constant_global"),66 /*sym_visibility=*/builder.getStringAttr("public"),67 /*type=*/TypeAttr::get(memrefType),68 /*initial_value=*/Attribute(),69 /*constant=*/builder.getUnitAttr(),70 /*alignment=*/IntegerAttr());71 72 auto globalVarIface =73 dyn_cast<GlobalVariableOpInterface>(constantGlobalOp->getOperation());74 ASSERT_TRUE(globalVarIface != nullptr);75 EXPECT_TRUE(globalVarIface.isConstant());76}77 78TEST_F(OpenACCOpsInterfacesTest, GlobalVariableOpInterfaceInitRegion) {79 // Test that memref::GlobalOp returns nullptr for getInitRegion()80 // since it uses attributes for initialization, not regions81 82 auto memrefType = MemRefType::get({10}, builder.getF32Type());83 OwningOpRef<memref::GlobalOp> globalOp = memref::GlobalOp::create(84 builder, loc,85 /*sym_name=*/builder.getStringAttr("test_global"),86 /*sym_visibility=*/builder.getStringAttr("private"),87 /*type=*/TypeAttr::get(memrefType),88 /*initial_value=*/Attribute(),89 /*constant=*/UnitAttr(),90 /*alignment=*/IntegerAttr());91 92 auto globalVarIface =93 dyn_cast<GlobalVariableOpInterface>(globalOp->getOperation());94 ASSERT_TRUE(globalVarIface != nullptr);95 96 // memref::GlobalOp doesn't have regions for initialization97 EXPECT_EQ(globalVarIface.getInitRegion(), nullptr);98}99 100//===----------------------------------------------------------------------===//101// AddressOfGlobalOpInterface Tests102//===----------------------------------------------------------------------===//103 104TEST_F(OpenACCOpsInterfacesTest, AddressOfGlobalOpInterfaceGetSymbol) {105 // Test that getSymbol() returns the correct symbol reference106 107 auto memrefType = MemRefType::get({5}, builder.getI32Type());108 const auto *symbolName = "test_global_symbol";109 110 OwningOpRef<memref::GetGlobalOp> getGlobalOp = memref::GetGlobalOp::create(111 builder, loc, memrefType, FlatSymbolRefAttr::get(&context, symbolName));112 113 auto addrOfGlobalIface =114 dyn_cast<AddressOfGlobalOpInterface>(getGlobalOp->getOperation());115 ASSERT_TRUE(addrOfGlobalIface != nullptr);116 EXPECT_EQ(addrOfGlobalIface.getSymbol().getLeafReference(), symbolName);117}118