Skip to content

Commit 663f8c1

Browse files
TIFitisronlieb
authored andcommitted
Fixed the crash in Target/LLVMIR/openmp-llvm.mlir by making linear clause lowering resilient when linear_var_types isn’t provided. LinearClauseProcessor now infers types from allocas/globals or uses the provided attribute, emits a clear error otherwise, and both wsloop/simd conversions use this helper (mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp).
1 parent fbea8fa commit 663f8c1

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "llvm/IR/DebugInfo.h"
3333
#include "llvm/IR/DebugInfoMetadata.h"
3434
#include "llvm/IR/DerivedTypes.h"
35+
#include "llvm/IR/GlobalValue.h"
3536
#include "llvm/IR/IRBuilder.h"
3637
#include "llvm/IR/InstIterator.h"
3738
#include "llvm/IR/MDBuilder.h"
@@ -153,13 +154,50 @@ class LinearClauseProcessor {
153154
llvm::BasicBlock *linearLastIterExitBB;
154155

155156
public:
157+
void registerType(llvm::Type *ty) { linearVarTypes.push_back(ty); }
158+
156159
// Register type for the linear variables
157160
void registerType(LLVM::ModuleTranslation &moduleTranslation,
158-
mlir::Attribute &ty) {
159-
linearVarTypes.push_back(moduleTranslation.convertType(
161+
mlir::Attribute ty) {
162+
registerType(moduleTranslation.convertType(
160163
mlir::cast<mlir::TypeAttr>(ty).getValue()));
161164
}
162165

166+
LogicalResult registerTypes(LLVM::ModuleTranslation &moduleTranslation,
167+
ValueRange linearVars,
168+
std::optional<ArrayAttr> linearVarTypesAttr,
169+
Operation *op) {
170+
if (linearVarTypesAttr) {
171+
for (Attribute linearVarType : *linearVarTypesAttr)
172+
registerType(moduleTranslation, linearVarType);
173+
return success();
174+
}
175+
176+
for (Value linearVar : linearVars) {
177+
llvm::Value *llvmVar = moduleTranslation.lookupValue(linearVar);
178+
if (!llvmVar) {
179+
op->emitError("missing translation for linear variable");
180+
return failure();
181+
}
182+
183+
if (auto *alloca = dyn_cast<llvm::AllocaInst>(llvmVar)) {
184+
registerType(alloca->getAllocatedType());
185+
continue;
186+
}
187+
188+
if (auto *global = dyn_cast<llvm::GlobalValue>(llvmVar)) {
189+
registerType(global->getValueType());
190+
continue;
191+
}
192+
193+
op->emitError(
194+
"linear_var_types attribute required to deduce linear variable type");
195+
return failure();
196+
}
197+
198+
return success();
199+
}
200+
163201
// Allocate space for linear variabes
164202
void createLinearVar(llvm::IRBuilderBase &builder,
165203
LLVM::ModuleTranslation &moduleTranslation,
@@ -2707,9 +2745,10 @@ convertOmpWsloop(Operation &opInst, llvm::IRBuilderBase &builder,
27072745
LinearClauseProcessor linearClauseProcessor;
27082746

27092747
if (!wsloopOp.getLinearVars().empty()) {
2710-
auto linearVarTypes = wsloopOp.getLinearVarTypes().value();
2711-
for (mlir::Attribute linearVarType : linearVarTypes)
2712-
linearClauseProcessor.registerType(moduleTranslation, linearVarType);
2748+
if (failed(linearClauseProcessor.registerTypes(
2749+
moduleTranslation, wsloopOp.getLinearVars(),
2750+
wsloopOp.getLinearVarTypes(), &opInst)))
2751+
return failure();
27132752

27142753
for (auto [idx, linearVar] : llvm::enumerate(wsloopOp.getLinearVars()))
27152754
linearClauseProcessor.createLinearVar(builder, moduleTranslation,
@@ -3030,9 +3069,10 @@ convertOmpSimd(Operation &opInst, llvm::IRBuilderBase &builder,
30303069
LinearClauseProcessor linearClauseProcessor;
30313070

30323071
if (!simdOp.getLinearVars().empty()) {
3033-
auto linearVarTypes = simdOp.getLinearVarTypes().value();
3034-
for (mlir::Attribute linearVarType : linearVarTypes)
3035-
linearClauseProcessor.registerType(moduleTranslation, linearVarType);
3072+
if (failed(linearClauseProcessor.registerTypes(
3073+
moduleTranslation, simdOp.getLinearVars(),
3074+
simdOp.getLinearVarTypes(), &opInst)))
3075+
return failure();
30363076
for (auto [idx, linearVar] : llvm::enumerate(simdOp.getLinearVars()))
30373077
linearClauseProcessor.createLinearVar(builder, moduleTranslation,
30383078
linearVar, idx);

0 commit comments

Comments
 (0)