diff --git a/lib/CodeGen/CGOpenMPRuntime.cpp b/lib/CodeGen/CGOpenMPRuntime.cpp
index 68b91c5be61e42cd293ce595c69186b3e2e03ab9..805faf425b3acd22daafa958be2d05f33a153306 100644
--- a/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -3808,20 +3808,6 @@ CGOpenMPRuntime::emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
                               Address Shareds, const OMPTaskDataTy &Data) {
   auto &C = CGM.getContext();
   llvm::SmallVector<PrivateDataTy, 4> Privates;
-#if 0
-  //Emit Affinity
-  if (AffinityExpr) {
-    CodeGenFunction::RunCleanupsScope AffinityScope(CGF);
-    auto Affinity = CGF.EmitScalarExpr(AffinityExpr,
-                                  /*IgnoreResultAssign*/ true);
-    // Build call __kmpc_omp_set_affinity(affinity)
-    llvm::Value *Args[] = {
-      CGF.Builder.CreateIntCast(Affinity, CGF.Int32Ty, /*isSigned*/ true)
-    };
-    CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_set_task_affinity),
-        Args);
-  }
-#endif
   // Aggregate privates and sort them by the alignment.
   auto I = Data.PrivateCopies.begin();
   for (auto *E : Data.PrivateVars) {
diff --git a/lib/CodeGen/CGStmtOpenMP.cpp b/lib/CodeGen/CGStmtOpenMP.cpp
index 2495cc7e5f0910a38dd265664d799672da422407..83336df911a29f73c8c39e3ae0a46807a997027f 100644
--- a/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/lib/CodeGen/CGStmtOpenMP.cpp
@@ -2474,6 +2474,20 @@ void CodeGenFunction::EmitOMPTaskBasedDirective(const OMPExecutableDirective &S,
     // By default the task is not final.
     Data.Final.setInt(/*IntVal=*/false);
   }
+  //Check if there is an affinity clause
+  // FIXME: to be clean this should probably be moved in OMPTaskDataTy
+  // and emitted in emitTaskCall...
+  if (const auto *AffinityClause = S.getSingleClause<OMPAffinityClause>()) {
+    auto Strict = AffinityClause->getStrict();
+    llvm::Value *StrictValue = Builder.getInt32(0);
+    if (Strict)
+      StrictValue = EvaluateExprAsBool(Strict);
+
+    CGM.getOpenMPRuntime().emitTaskAffinityClause(*this,
+                           AffinityClause->getAffinityKind(),
+                           AffinityClause->getAffinity(),
+                           StrictValue);
+  }
   // Check if the task has 'priority' clause.
   if (const auto *Clause = S.getSingleClause<OMPPriorityClause>()) {
     // Runtime currently does not support codegen for priority clause argument.
@@ -2632,20 +2646,6 @@ void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) {
                                             SharedsTy, CapturedStruct, IfCond,
                                             Data);
   };
-#if 0
-  //Check if there is an affinity clause
-  if (const auto *AffinityClause = S.getSingleClause<OMPAffinityClause>()) {
-    auto Strict = AffinityClause->getStrict();
-    llvm::Value *StrictValue = Builder.getInt32(0);
-    if (Strict)
-      StrictValue = EvaluateExprAsBool(Strict);
-
-    CGM.getOpenMPRuntime().emitTaskAffinityClause(*this,
-                           AffinityClause->getAffinityKind(),
-                           AffinityClause->getAffinity(),
-                           StrictValue);
-  }
-#endif
   EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data);
 }