Skip to content

[CIR] Add PointerLikeType interface support for cir::PointerType #139768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===----------------------------------------------------------------------===//
//
// 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 contains external dialect interfaces for CIR.
//
//===----------------------------------------------------------------------===//

#ifndef CLANG_CIR_DIALECT_OPENACC_CIROPENACCTYPEINTERFACES_H
#define CLANG_CIR_DIALECT_OPENACC_CIROPENACCTYPEINTERFACES_H

#include "mlir/Dialect/OpenACC/OpenACC.h"

namespace cir::acc {

template <typename T>
struct OpenACCPointerLikeModel
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silly question perhaps, why is this not part of MLIR? It doesn't seem to refer to CIR at all? Or am I missing something?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: public mlir::acc::PointerLikeType::ExternalModel<
OpenACCPointerLikeModel<T>, T> {
mlir::Type getElementType(mlir::Type pointer) const {
return mlir::cast<T>(pointer).getPointee();
}
mlir::acc::VariableTypeCategory
getPointeeTypeCategory(mlir::Type pointer,
mlir::TypedValue<mlir::acc::PointerLikeType> varPtr,
mlir::Type varType) const;
};

} // namespace cir::acc

#endif // CLANG_CIR_DIALECT_OPENACC_CIROPENACCTYPEINTERFACES_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef CLANG_CIR_DIALECT_OPENACC_REGISTEROPENACCEXTENSIONS_H
#define CLANG_CIR_DIALECT_OPENACC_REGISTEROPENACCEXTENSIONS_H

namespace mlir {
class DialectRegistry;
} // namespace mlir

namespace cir::acc {

void registerOpenACCExtensions(mlir::DialectRegistry &registry);

} // namespace cir::acc

#endif // CLANG_CIR_DIALECT_OPENACC_REGISTEROPENACCEXTENSIONS_H
7 changes: 7 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "clang/AST/DeclGroup.h"
#include "clang/CIR/CIRGenerator.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/OpenACC/RegisterOpenACCExtensions.h"

using namespace cir;
using namespace clang;
Expand All @@ -38,6 +39,12 @@ void CIRGenerator::Initialize(ASTContext &astContext) {
mlirContext = std::make_unique<mlir::MLIRContext>();
mlirContext->loadDialect<cir::CIRDialect>();
mlirContext->getOrLoadDialect<mlir::acc::OpenACCDialect>();

// Register extensions to integrate CIR types with OpenACC.
mlir::DialectRegistry registry;
cir::acc::registerOpenACCExtensions(registry);
mlirContext->appendDialectRegistry(registry);

cgm = std::make_unique<clang::CIRGen::CIRGenModule>(
*mlirContext.get(), astContext, codeGenOpts, diags);
}
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CIR/CodeGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ add_clang_library(clangCIR
clangBasic
clangLex
${dialect_libs}
CIROpenACCSupport
MLIRCIR
MLIRCIRInterfaces
)
1 change: 1 addition & 0 deletions clang/lib/CIR/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(IR)
add_subdirectory(OpenACC)
add_subdirectory(Transforms)
65 changes: 65 additions & 0 deletions clang/lib/CIR/Dialect/OpenACC/CIROpenACCTypeInterfaces.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Implementation of external dialect interfaces for CIR.
//
//===----------------------------------------------------------------------===//

#include "clang/CIR/Dialect/OpenACC/CIROpenACCTypeInterfaces.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"

namespace cir::acc {

mlir::Type getBaseType(mlir::Value varPtr) {
mlir::Operation *op = varPtr.getDefiningOp();
assert(op && "Expected a defining operation");

// This is the variable definition we're looking for.
if (auto allocaOp = mlir::dyn_cast<cir::AllocaOp>(*op))
return allocaOp.getAllocaType();

// Look through casts to the source pointer.
if (auto castOp = mlir::dyn_cast<cir::CastOp>(*op))
return getBaseType(castOp.getSrc());

// Follow the source of ptr strides.
if (auto ptrStrideOp = mlir::dyn_cast<cir::PtrStrideOp>(*op))
return getBaseType(ptrStrideOp.getBase());

if (auto getMemberOp = mlir::dyn_cast<cir::GetMemberOp>(*op))
return getBaseType(getMemberOp.getAddr());

return mlir::cast<cir::PointerType>(varPtr.getType()).getPointee();
}

template <>
mlir::acc::VariableTypeCategory
OpenACCPointerLikeModel<cir::PointerType>::getPointeeTypeCategory(
mlir::Type pointer, mlir::TypedValue<mlir::acc::PointerLikeType> varPtr,
mlir::Type varType) const {
mlir::Type eleTy = getBaseType(varPtr);

if (auto mappableTy = mlir::dyn_cast<mlir::acc::MappableType>(eleTy))
return mappableTy.getTypeCategory(varPtr);

if (isAnyIntegerOrFloatingPointType(eleTy) ||
mlir::isa<cir::BoolType>(eleTy) || mlir::isa<cir::PointerType>(eleTy))
return mlir::acc::VariableTypeCategory::scalar;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the CIR type system allows computation of interior pointers: https://godbolt.org/z/aPn1j5a69
Which means that scalar categorization might capture things which it shouldn't. More specifically, "a member of a composite variable" is not considered a "scalar" in OpenACC terminology - and the CIR type system by itself does not provide the appropriate features to distinguish this.

Here is how we dealt with this case in FIR which allows the same: https://github.com/llvm/llvm-project/blob/main/flang/lib/Optimizer/OpenACC/FIROpenACCTypeInterfaces.cpp#L386

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so it's not just a property of the type, but an interpretation of the type within the context of the value it is associated with. That makes sense now for the extra parameters that I'm ignoring here.

if (mlir::isa<cir::ArrayType>(eleTy))
return mlir::acc::VariableTypeCategory::array;
if (mlir::isa<cir::RecordType>(eleTy))
return mlir::acc::VariableTypeCategory::composite;
if (mlir::isa<cir::FuncType>(eleTy) || mlir::isa<cir::VectorType>(eleTy))
return mlir::acc::VariableTypeCategory::nonscalar;

// Without further checking, this type cannot be categorized.
return mlir::acc::VariableTypeCategory::uncategorized;
}

} // namespace cir::acc
12 changes: 12 additions & 0 deletions clang/lib/CIR/Dialect/OpenACC/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
add_clang_library(CIROpenACCSupport
CIROpenACCTypeInterfaces.cpp
RegisterOpenACCExtensions.cpp

DEPENDS
MLIRCIRTypeConstraintsIncGen

LINK_LIBS PUBLIC
MLIRIR
MLIRCIR
MLIROpenACCDialect
)
27 changes: 27 additions & 0 deletions clang/lib/CIR/Dialect/OpenACC/RegisterOpenACCExtensions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Registration for OpenACC extensions as applied to CIR dialect.
//
//===----------------------------------------------------------------------===//

#include "clang/CIR/Dialect/OpenACC/RegisterOpenACCExtensions.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "clang/CIR/Dialect/OpenACC/CIROpenACCTypeInterfaces.h"

namespace cir::acc {

void registerOpenACCExtensions(mlir::DialectRegistry &registry) {
registry.addExtension(+[](mlir::MLIRContext *ctx, cir::CIRDialect *dialect) {
cir::PointerType::attachInterface<
OpenACCPointerLikeModel<cir::PointerType>>(*ctx);
});
}

} // namespace cir::acc
16 changes: 16 additions & 0 deletions clang/unittests/CIR/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include )
set(MLIR_TABLEGEN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/tools/mlir/include)
include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
include_directories(${MLIR_TABLEGEN_OUTPUT_DIR})

add_distinct_clang_unittest(CIRUnitTests
PointerLikeTest.cpp
LLVM_COMPONENTS
Core

LINK_LIBS
MLIRCIR
CIROpenACCSupport
MLIRIR
MLIROpenACCDialect
)
Loading
Loading