|
32 | 32 | #include "llvm/IR/DebugInfo.h" |
33 | 33 | #include "llvm/IR/DebugInfoMetadata.h" |
34 | 34 | #include "llvm/IR/DerivedTypes.h" |
| 35 | +#include "llvm/IR/GlobalValue.h" |
35 | 36 | #include "llvm/IR/IRBuilder.h" |
36 | 37 | #include "llvm/IR/InstIterator.h" |
37 | 38 | #include "llvm/IR/MDBuilder.h" |
@@ -153,13 +154,50 @@ class LinearClauseProcessor { |
153 | 154 | llvm::BasicBlock *linearLastIterExitBB; |
154 | 155 |
|
155 | 156 | public: |
| 157 | + void registerType(llvm::Type *ty) { linearVarTypes.push_back(ty); } |
| 158 | + |
156 | 159 | // Register type for the linear variables |
157 | 160 | void registerType(LLVM::ModuleTranslation &moduleTranslation, |
158 | | - mlir::Attribute &ty) { |
159 | | - linearVarTypes.push_back(moduleTranslation.convertType( |
| 161 | + mlir::Attribute ty) { |
| 162 | + registerType(moduleTranslation.convertType( |
160 | 163 | mlir::cast<mlir::TypeAttr>(ty).getValue())); |
161 | 164 | } |
162 | 165 |
|
| 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 | + |
163 | 201 | // Allocate space for linear variabes |
164 | 202 | void createLinearVar(llvm::IRBuilderBase &builder, |
165 | 203 | LLVM::ModuleTranslation &moduleTranslation, |
@@ -2707,9 +2745,10 @@ convertOmpWsloop(Operation &opInst, llvm::IRBuilderBase &builder, |
2707 | 2745 | LinearClauseProcessor linearClauseProcessor; |
2708 | 2746 |
|
2709 | 2747 | 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(); |
2713 | 2752 |
|
2714 | 2753 | for (auto [idx, linearVar] : llvm::enumerate(wsloopOp.getLinearVars())) |
2715 | 2754 | linearClauseProcessor.createLinearVar(builder, moduleTranslation, |
@@ -3030,9 +3069,10 @@ convertOmpSimd(Operation &opInst, llvm::IRBuilderBase &builder, |
3030 | 3069 | LinearClauseProcessor linearClauseProcessor; |
3031 | 3070 |
|
3032 | 3071 | 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(); |
3036 | 3076 | for (auto [idx, linearVar] : llvm::enumerate(simdOp.getLinearVars())) |
3037 | 3077 | linearClauseProcessor.createLinearVar(builder, moduleTranslation, |
3038 | 3078 | linearVar, idx); |
|
0 commit comments